Active Model Validations

Provides a full validation framework to your objects.

A minimal implementation could be:

  class Person
    include ActiveModel::Validations

    attr_accessor :first_name, :last_name

    validates_each :first_name, :last_name do |record, attr, value|
      record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
    end
  end

Which provides you with the full standard validation stack that you know from Active Record:

  person = Person.new
  person.valid?                   # => true
  person.invalid?                 # => false

  person.first_name = 'zoolander'
  person.valid?                   # => false
  person.invalid?                 # => true
  person.errors                   # => #<OrderedHash {:first_name=>["starts with z."]}>

Note that ActiveModel::Validations automatically adds an errors method to your instances initialized with a new ActiveModel::Errors object, so there is no need for you to do this manually.


Active Model Numericality Validator


Active Model Presence Validator


Active Model Length Validator


Active Model Confirmation Validator


Active Model validates method


Active Model Acceptance Validator


Active Model Exclusion Validator


Active Model Format Validator


Active Model Inclusion Validator

Methods
E
I
R
V
Included Modules
Classes and Modules
Instance Public methods
errors()

Returns the Errors object that holds all information about attribute error messages.

     # File activemodel/lib/active_model/validations.rb, line 183
183:     def errors
184:       @errors ||= Errors.new(self)
185:     end
invalid?(context = nil)

Performs the opposite of valid?. Returns true if errors were added, false otherwise.

     # File activemodel/lib/active_model/validations.rb, line 200
200:     def invalid?(context = nil)
201:       !valid?(context)
202:     end
valid?(context = nil)

Runs all the specified validations and returns true if no errors were added otherwise false. Context can optionally be supplied to define which callbacks to test against (the context is defined on the validations using :on).

     # File activemodel/lib/active_model/validations.rb, line 190
190:     def valid?(context = nil)
191:       current_context, self.validation_context = validation_context, context
192:       errors.clear
193:       run_validations!
194:     ensure
195:       self.validation_context = current_context
196:     end
validates_with(*args, &block)

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 Person
    include ActiveModel::Validations

    validate :instance_validations, :on => :create

    def instance_validations
      validates_with MyValidator, MyOtherValidator
    end
  end

Standard configuration options (:on, :if and :unless), which are available on the class version of validates_with, should instead be placed on the validates method as these are applied and tested in the callback.

If you pass any additional configuration options, they will be passed to the class and available as options, please refer to the class version of this method for more information.

     # File activemodel/lib/active_model/validations/with.rb, line 134
134:     def validates_with(*args, &block)
135:       options = args.extract_options!
136:       args.each do |klass|
137:         validator = klass.new(options, &block)
138:         validator.validate(self)
139:       end
140:     end
Instance Protected methods
run_validations!()
     # File activemodel/lib/active_model/validations.rb, line 225
225:     def run_validations!
226:       run_callbacks :validate
227:       errors.empty?
228:     end