validates_format_of

validates_format_of(*attr_names) Instance Public methods Validates whether the value of the specified attribute is of the correct form, going by the regular expression provided.You can require that the attribute matches the regular expression: class Person < ActiveRecord::Base validates_format_of :email, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, on: :create end Alternatively, you can require that the specified attribute does not match the regular expression: class

validates_exclusion_of

validates_exclusion_of(*attr_names) Instance Public methods Validates that the value of the specified attribute is not in a particular enumerable object. class Person < ActiveRecord::Base validates_exclusion_of :username, in: %w( admin superuser ), message: "You don't belong here" validates_exclusion_of :age, in: 30..60, message: 'This site is only for under 30 and over 60' validates_exclusion_of :format, in: %w( mov avi ), message: "extension %{value} is not allowed" va

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

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_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

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={}>, # ]

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

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

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!

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