validate

validate(*args, &block) Instance Public methods Adds a validation method or block to the class. This is useful when overriding the validate instance method becomes too unwieldy and you're looking for more descriptive declaration of your validations. This can be done with a symbol pointing to a method: class Comment include ActiveModel::Validations validate :must_be_friends def must_be_friends errors.add(:base, 'Must be friends to leave a comment') unless commenter.f

validates

validates(*attributes) Instance Public methods This method is a shortcut to all default validators and any custom validator classes ending in 'Validator'. Note that Rails default validators can be overridden inside specific classes by creating custom validator classes in their place such as PresenceValidator. Examples of using the default rails validators: validates :terms, acceptance: true validates :password, confirmation: true validates :username, exclusion: { in: %w(admin super

validates!

validates!(*attributes) Instance Public methods This method is used to define validations that cannot be corrected by end users and are considered exceptional. So each validator defined with bang or :strict option set to true will always raise ActiveModel::StrictValidationFailed instead of adding error when validation fails. See validates for more information about the validation itself. class Person include ActiveModel::Validations attr_accessor :name validates! :name, pres

validates_each

validates_each(*attr_names, &block) Instance Public methods Validates each attribute against a block. class Person include ActiveModel::Validations attr_accessor :first_name, :last_name validates_each :first_name, :last_name, allow_blank: true do |record, attr, value| record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z end end Options: :on - Specifies the contexts where this validation is active. You can pass a symbol or an array of symbols. (e.g. on

validates_with

validates_with(*args, &block) Instance Public methods Passes the record off to the class or classes specified and allows them to add errors based on more complex conditions. class Person include ActiveModel::Validations validates_with MyValidator end class MyValidator < ActiveModel::Validator def validate(record) if some_complex_logic record.errors.add :base, 'This record is invalid' end end private def some_complex_logic # ... end end

validators

validators() Instance Public methods List all validators that are being used to validate the model using validates_with method. class Person include ActiveModel::Validations validates_with MyValidator validates_with OtherValidator, on: :create validates_with StrictValidator, strict: true end Person.validators # => [ # #<MyValidator:0x007fbff403e808 @options={}>, # #<OtherValidator:0x007fbff403d930 @options={on: :create}>, # #<StrictValidat

validators_on

validators_on(*attributes) Instance Public methods List all validators that are being used to validate a specific attribute. class Person include ActiveModel::Validations attr_accessor :name , :age validates_presence_of :name validates_inclusion_of :age, in: 0..99 end Person.validators_on(:name) # => [ # #<ActiveModel::Validations::PresenceValidator:0x007fe604914e60 @attributes=[:name], @options={}>, # ]

validates_absence_of

validates_absence_of(*attr_names) Instance Public methods Validates that the specified attributes are blank (as defined by Object#blank?). Happens by default on save. class Person < ActiveRecord::Base validates_absence_of :first_name end The first_name attribute must be in the object and it must be blank. Configuration options: :message - A custom error message (default is: âmust be blankâ). There is also a list of default options supported by every validator: :if, :unless

validates_acceptance_of

validates_acceptance_of(*attr_names) Instance Public methods Encapsulates the pattern of wanting to validate the acceptance of a terms of service check box (or similar agreement). class Person < ActiveRecord::Base validates_acceptance_of :terms_of_service validates_acceptance_of :eula, message: 'must be abided' end If the database column does not exist, the terms_of_service attribute is entirely virtual. This check is performed only if terms_of_service is not nil and by def

validates_confirmation_of

validates_confirmation_of(*attr_names) Instance Public methods Encapsulates the pattern of wanting to validate a password or email address field with a confirmation. Model: class Person < ActiveRecord::Base validates_confirmation_of :user_name, :password validates_confirmation_of :email_address, message: 'should match confirmation' end View: <%= password_field "person", "password" %> <%= password_field "person", "password_c