### Define and use CreateUserCommand Source: https://github.com/simple-and-powerful/act-form/blob/master/README.md This example defines a CreateUserCommand that inherits from ActForm::Command and combines UserForm. It shows how to set up initial attributes and the `perform` method for creating a user. It also illustrates the correct way to run a command object. ```ruby class CreateUserCommand < ActForm::Command combine UserForm # Do some pre-inialize actions. setup do @name = 'foo' end def perform attributes[:name] = @name User.create(attributes) end end # CreateUserCommand.new # => NoMethodError: private method `new' called for CreateUserCommand:Class command = CreateUserCommand.run(phone: '12345678901') if command.success? @user = command.result # do something... else command.errors.full_messages # => ["Email can't be blank"] # do something... end ``` -------------------------------- ### Combine PhoneForm and EmailForm into UserForm Source: https://github.com/simple-and-powerful/act-form/blob/master/README.md This example shows how to combine multiple forms (PhoneForm and EmailForm) into a single UserForm using the `combine` method. It demonstrates how validations from combined forms are aggregated. ```ruby class PhoneForm < ActForm::Base attribute :phone validates_format_of :phone, with: /\A\d{11}\z/i end class EmailForm < ActForm::Base attribute :email validate :check_email def check_email errors.add(:email, :blank) if email.blank? end end class UserForm < ActForm::Base combine PhoneForm, EmailForm end user_form = UserForm.new user_form.valid? user_form.errors.full_messages # => ["Phone is invalid", "Email can't be blank"] UserForm.new(phone: '12345678901', email: '1').valid? # => true ``` -------------------------------- ### Save PhoneForm data to a target object Source: https://github.com/simple-and-powerful/act-form/blob/master/README.md This example shows the `save` method, which syncs attributes to a target object and then calls the target's `save` method if validations pass. It also demonstrates the `persisted?` method. ```ruby target = Class.new do attr_accessor :phone attr_reader :saved def save @saved = true end end.new form = PhoneForm.new(phone: '12345678901') form.save(target) # same as form.sync(target) and target.save target.phone # => '12345678901' target.saved # => true form.persisted? # => true ``` -------------------------------- ### Validate PhoneForm format Source: https://github.com/simple-and-powerful/act-form/blob/master/README.md This example shows how to define a PhoneForm with a format validation for the 'phone' attribute using Active Model's `validates_format_of`. It demonstrates checking validity and accessing error messages. ```ruby class PhoneForm < ActForm::Base attribute :phone validates_format_of :phone, with: /\A\d{11}\z/i end form = PhoneForm.new form.valid? # => false form.errors.full_messages # => ["Phone is invalid"] PhoneForm.new(phone: '12345678901').valid? # => true ``` -------------------------------- ### Initialize PhoneForm from a target object Source: https://github.com/simple-and-powerful/act-form/blob/master/README.md This snippet demonstrates the `init_by` method, which copies attributes from a target object to the form and sets the target as the default for subsequent save operations. It shows initializing a form and then saving it. ```ruby target = Class.new do attr_accessor :phone attr_reader :saved def save @saved = true end end.new target.phone = '12345678901' form = PhoneForm.new form.init_by(target) form.phone # => '12345678901' form.save # => true target.saved # => true ``` -------------------------------- ### Define UserForm with attribute API Source: https://github.com/simple-and-powerful/act-form/blob/master/README.md This snippet illustrates defining a UserForm using ActForm's attribute API. It sets up attributes with options like `required`, `type`, and `default`, and shows how to instantiate and use the form. ```ruby class UserForm < ActForm::Base attribute :name, required: true attribute :age, type: :integer attribute :address attribute :nickname, default: 'nick' attribute :desc, default: ->{ 'desc' } end form = UserForm.new(name: 'su', age: '18', address: 'somewhere') form.name # => 'su' form.age # => 18 form.address # => 'somewhere' form.nickname # => 'nick' form.desc # => 'desc' # override default form.nickname = 'hello' form.nickname # => 'hello' # required form = UserForm.new(age: '18', address: 'somewhere') form.valid? # => false form.errors.full_messages # => ["Name require a value"] ``` -------------------------------- ### Combine PhoneForm into AdminForm Source: https://github.com/simple-and-powerful/act-form/blob/master/README.md This snippet demonstrates combining a single form (PhoneForm) into an AdminForm. It shows how the validations from the PhoneForm are applied to the AdminForm. ```ruby class PhoneForm < ActForm::Base attribute :phone validates_format_of :phone, with: /\A\d{11}\z/i end class AdminForm < ActForm::Base combine PhoneForm end admin_form = AdminForm.new admin_form.valid? admin_form.errors.full_messages # => ["Phone is invalid"] AdminForm.new(phone: '12345678901').valid? # => true ``` -------------------------------- ### Define UserForm with dry-schema parameters Source: https://github.com/simple-and-powerful/act-form/blob/master/README.md This snippet demonstrates how to define a UserForm using ActForm's integration with dry-schema. It specifies required and optional parameters with descriptions and default values. ```ruby class UserForm < ActForm::Base params do required(:name).filled.desc('Name') optional(:age).value(:integer).desc('Age') optional(:address).desc('Address') optional(:nickname).default('nick').desc('Nick') # below will support in the future # attribute :desc, default: ->{ 'desc' } end end ``` -------------------------------- ### Integrate UserForm with Grape API Source: https://github.com/simple-and-powerful/act-form/blob/master/README.md This code shows how to integrate a UserForm with a Grape API. It uses the `contract` method to apply the form's schema to API endpoints, allowing for request validation. ```ruby module WorkWithGrapeSpec class API < Grape::API format :json contract UserForm.contract get '/foo' do 'hello world' end contract UserForm.contract do required(:desc).filled end get '/bar' do 'hello world' end end end ``` -------------------------------- ### Sync attributes from PhoneForm to a target object Source: https://github.com/simple-and-powerful/act-form/blob/master/README.md This snippet demonstrates the `sync` method of ActForm, which copies attributes from a form object to a target object without triggering validations. It shows how to create a target object and sync data to it. ```ruby target = Class.new do attr_accessor :phone end.new form = PhoneForm.new(phone: '12345678901') form.sync(target) target.phone # => '12345678901' ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.