EachValidator is a validator which iterates through the attributes given in the options hash invoking the validate_each method passing in the record, attribute and value.

All Active Model validations are built on top of this Validator.

Methods
Attributes
[R] attributes
Public Class methods
new(options)

Returns a new validator instance. All options will be available via the options reader, however the :attributes option will be removed and instead be made available through the attributes reader.

       # File activemodel/lib/active_model/validator.rb, line 140
140:     def initialize(options)
141:       @attributes = Array.wrap(options.delete(:attributes))
142:       raise ":attributes cannot be blank" if @attributes.empty?
143:       super
144:       check_validity!
145:     end
Public Instance methods
check_validity!()

Hook method that gets called by the initializer allowing verification that the arguments supplied are valid. You could for example raise an ArgumentError when invalid options are supplied.

       # File activemodel/lib/active_model/validator.rb, line 167
167:     def check_validity!
168:     end
validate(record)

Performs validation on the supplied record. By default this will call validates_each to determine validity therefore subclasses should override validates_each with validation logic.

       # File activemodel/lib/active_model/validator.rb, line 150
150:     def validate(record)
151:       attributes.each do |attribute|
152:         value = record.read_attribute_for_validation(attribute)
153:         next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])
154:         validate_each(record, attribute, value)
155:       end
156:     end
validate_each(record, attribute, value)

Override this method in subclasses with the validation logic, adding errors to the records errors array where necessary.

       # File activemodel/lib/active_model/validator.rb, line 160
160:     def validate_each(record, attribute, value)
161:       raise NotImplementedError
162:     end