Type:
Module

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.messages          # => {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 Length Validator

validates_with
  • References/Ruby on Rails/Rails/Classes/ActiveModel/ActiveModel::Validations/ActiveModel::Validations::ClassMethods

validates_with(*args, &block) Instance Public methods Passes the record

2025-01-10 15:47:30
clear_validators!
  • References/Ruby on Rails/Rails/Classes/ActiveModel/ActiveModel::Validations/ActiveModel::Validations::ClassMethods

clear_validators!() Instance Public methods Clears all of the validators and

2025-01-10 15:47:30
validates_exclusion_of
  • References/Ruby on Rails/Rails/Classes/ActiveModel/ActiveModel::Validations/ActiveModel::Validations::HelperMethods

validates_exclusion_of(*attr_names) Instance Public methods Validates that the

2025-01-10 15:47:30
before_validation
  • References/Ruby on Rails/Rails/Classes/ActiveModel/ActiveModel::Validations/ActiveModel::Validations::Callbacks/ActiveModel::Validations::Callbacks::ClassMethods

before_validation(*args, &block) Instance Public methods Defines a callback

2025-01-10 15:47:30
validates_size_of
  • References/Ruby on Rails/Rails/Classes/ActiveModel/ActiveModel::Validations/ActiveModel::Validations::HelperMethods

validates_size_of(*attr_names) Instance Public methods Alias for:

2025-01-10 15:47:30