module Validations

Ruby on Rails 3.2.22.5

Since v2.2.3

Available in: v2.2.3 v2.3.18 v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

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 Acceptance Validator

Active Model Confirmation Validator

Active Model Exclusion Validator

Active Model Format Validator

Active Model Inclusion Validator

Active Model Length Validator

Active Model Numericality Validator

Active Model Presence Validator

Active Model validates method

Namespace

Modules

Classes

Includes

Extends

Attributes

Methods (defined here)

Used by

Included by (1)

Methods (inherited)

From ActiveModel::Translation (3)
From ActiveModel::Validations::HelperMethods (9)
From ActiveSupport::Callbacks (1)
From ActiveSupport::Concern (3)
From ActiveModel::Naming (9)
From ActiveSupport::DescendantsTracker (6)

Type at least 2 characters to search.

↑↓ navigate · open · esc close