kind

kind() Class Public methods Returns the kind of the validator. PresenceValidator.kind # => :presence UniquenessValidator.kind # => :uniqueness

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 validate :instance_validations def instance_validations validates_with MyValidator end end Please consult the class method documentation for more information on creating your own validator. You may also pass it multiple classes, like so: class Per

valid?

valid?(context = nil) Instance Public methods Runs all the specified validations and returns true if no errors were added otherwise false. class Person include ActiveModel::Validations attr_accessor :name validates_presence_of :name end person = Person.new person.name = '' person.valid? # => false person.name = 'david' person.valid? # => true Context can optionally be supplied to define which callbacks to test against (the context is defined on the validations using

invalid?

invalid?(context = nil) Instance Public methods Performs the opposite of valid?. Returns true if errors were added, false otherwise. class Person include ActiveModel::Validations attr_accessor :name validates_presence_of :name end person = Person.new person.name = '' person.invalid? # => true person.name = 'david' person.invalid? # => false Context can optionally be supplied to define which callbacks to test against (the context is defined on the validations using :o

errors

errors() Instance Public methods Returns the Errors object that holds all information about attribute error messages. class Person include ActiveModel::Validations attr_accessor :name validates_presence_of :name end person = Person.new person.valid? # => false person.errors # => #<ActiveModel::Errors:0x007fe603816640 @messages={name:["can't be blank"]}>

validates_size_of

validates_size_of(*attr_names) Instance Public methods Alias for: validates_length_of

validates_presence_of

validates_presence_of(*attr_names) Instance Public methods Validates that the specified attributes are not blank (as defined by Object#blank?). Happens by default on save. class Person < ActiveRecord::Base validates_presence_of :first_name end The first_name attribute must be in the object and it cannot be blank. If you want to validate the presence of a boolean field (where the real values are true and false), you will want to use validates_inclusion_of :field_name, in: [tru

validates_numericality_of

validates_numericality_of(*attr_names) Instance Public methods Validates whether the value of the specified attribute is numeric by trying to convert it to a float with Kernel.Float (if only_integer is false) or applying it to the regular expression /\A[+\-]?\d+\Z/ (if only_integer is set to true). class Person < ActiveRecord::Base validates_numericality_of :value, on: :create end Configuration options: :message - A custom error message (default is: âis not a numberâ). :on

validates_length_of

validates_length_of(*attr_names) Instance Public methods Validates that the specified attribute matches the length restrictions supplied. Only one option can be used at a time: class Person < ActiveRecord::Base validates_length_of :first_name, maximum: 30 validates_length_of :last_name, maximum: 30, message: "less than 30 if you don't mind" validates_length_of :fax, in: 7..32, allow_nil: true validates_length_of :phone, in: 7..32, allow_blank: true validates_length_of

validates_inclusion_of

validates_inclusion_of(*attr_names) Instance Public methods Validates whether the value of the specified attribute is available in a particular enumerable object. class Person < ActiveRecord::Base validates_inclusion_of :gender, in: %w( m f ) validates_inclusion_of :age, in: 0..99 validates_inclusion_of :format, in: %w( jpg gif png ), message: "extension %{value} is not included in the list" validates_inclusion_of :states, in: ->(person) { STATES[person.country] } v