class ActiveModel::EachValidator
All Active Model validations are built on top of this validator.
record, attribute, and value.
in the options hash invoking the validate_each
method passing in theEachValidator
is a validator which iterates through the attributes given
= Active Model EachValidator
def check_validity!
that the arguments supplied are valid. You could for example raise an
Hook method that gets called by the initializer allowing verification
def check_validity! end
def initialize(options)
+options+ reader, however the :attributes option will be removed
Returns a new validator instance. All options will be available via the
def initialize(options) @attributes = Array(options.delete(:attributes)) raise ArgumentError, ":attributes cannot be blank" if @attributes.empty? super check_validity! end
def prepare_value_for_validation(value, record, attr_name)
def prepare_value_for_validation(value, record, attr_name) value end
def validate(record)
+validate_each+ to determine validity therefore subclasses should
Performs validation on the supplied record. By default this will call
def validate(record) attributes.each do |attribute| value = record.read_attribute_for_validation(attribute) next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank]) value = prepare_value_for_validation(value, record, attribute) validate_each(record, attribute, value) end end
def validate_each(record, attribute, value)
Override this method in subclasses with the validation logic, adding
def validate_each(record, attribute, value) raise NotImplementedError, "Subclasses must implement a validate_each(record, attribute, value) method" end