module

ActiveModel::Validations

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.

Included modules

  • ActiveSupport::Callbacks
  • HelperMethods

Files

  • activemodel/lib/active_model/validations.rb
  • activemodel/lib/active_model/validations/acceptance.rb
  • activemodel/lib/active_model/validations/callbacks.rb
  • activemodel/lib/active_model/validations/confirmation.rb
  • activemodel/lib/active_model/validations/exclusion.rb
  • activemodel/lib/active_model/validations/format.rb
  • activemodel/lib/active_model/validations/inclusion.rb
  • activemodel/lib/active_model/validations/length.rb
  • activemodel/lib/active_model/validations/numericality.rb
  • activemodel/lib/active_model/validations/presence.rb
  • activemodel/lib/active_model/validations/validates.rb
  • activemodel/lib/active_model/validations/with.rb

Nested classes and modules