The simple way to create form objects or command/service objects with ActiveModel
Latest commit bb619bd Jan 4, 2017 @zires zires change README
Failed to load latest commit information.
lib change name to act_form Dec 13, 2016
test change name to act_form Dec 13, 2016
.gitignore update gitignore Nov 4, 2016 Update Dec 21, 2016
Gemfile change name to act_form Dec 13, 2016
LICENSE.txt first commit Oct 31, 2016 change README Jan 4, 2017
Rakefile first commit Oct 31, 2016
act_form.gemspec change name to act_form Dec 13, 2016


ActForm is the gem that provide a simple way to create form object or command object or service object, it only depends on activemodel >= 5 and provides few api.


API - attribute

class UserForm < ActForm::Base
  attribute :name, required: true
  attribute :age,  type: :integer
  attribute :address
  attribute :nickname, default: 'nick'
  attribute :desc, default: ->{ 'desc' }

form = 'su', age: '18', address: 'somewhere') # => 'su'
form.age # => 18
form.address # => 'somewhere'
form.nickname # => 'nick'
form.desc # => 'desc'
# override default
form.nickname = 'hello'
form.nickname # => 'hello'

# required
form = '18', address: 'somewhere')
form.valid? # => false
form.errors.full_messages # => ["Name require a value"]

Difference between required and validates_presence_of

required will run before the validation, and it will cancel other validations if return false.

form object

API - valid?

Compliant with the active model api

class PhoneForm < ActForm::Base
  attribute :phone

  validates_format_of :phone, with: /\A\d{11}\z/i

form =
form.valid? # => false
form.errors.full_messages # => ["Phone is invalid"] '12345678901').valid? # => true

API - sync

sync only copy attributes to target, will not trigger validate

target = do
  attr_accessor :phone

form = '12345678901')
form.sync(target) # => '12345678901'

API - save

sync to the target and call the save method when passed the validation

target = do
  attr_accessor :phone
  attr_reader :saved

  def save
    @saved = true

form = '12345678901') # same as form.sync(target) and # => '12345678901'
target.saved # => true
form.persisted? # => true

API - init_by

init_by will copy attributes form target to the form, and set default target.

target = do
  attr_accessor :phone
  attr_reader :saved

  def save
    @saved = true
  end = '12345678901'

form =
form.init_by(target) # => '12345678901' # => true
target.saved # => true

API - combine

form can combine to other forms

class PhoneForm < ActForm::Base
  attribute :phone
  validates_format_of :phone, with: /\A\d{11}\z/i

class EmailForm < ActForm::Base
  attribute :email

  validate :check_email

  def check_email
    errors.add(:email, :blank) if email.blank?

class UserForm < ActForm::Base
  combine PhoneForm, EmailForm

class AdminForm < ActForm::Base
  combine PhoneForm

user_form =
user_form.errors.full_messages # => ["Phone is invalid", "Email can't be blank"] '12345678901', email: '1').valid? # => true
admin_form =
admin_form.errors.full_messages # => ["Phone is invalid"] '12345678901').valid? # => true

command/service object

Command object almost like form object. Command object can't init by new, and it has some new features.

API - perform, run, success?, failure?

command object must respond to perform method.

class CreateUserCommand < ActForm::Command
  combine UserForm

  def perform
# => NoMethodError: private method `new' called for CreateUserCommand:Class 

command = '12345678901')
if command.success?
  @user = command.result
  # do something...
  command.errors.full_messages # => ["Email can't be blank"]
  # do something...


Add this line to your application's Gemfile:

gem 'act_form'

And then execute:

$ bundle

Or install it yourself as:

$ gem install act_form


To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to


Bug reports and pull requests are welcome on GitHub at[USERNAME]/act_form. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.


The gem is available as open source under the terms of the MIT License.