Class: ActiveModel::EachValidator
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
ActiveModel::BlockValidator, ActiveModel::Validations::AbsenceValidator, ActiveModel::Validations::AcceptanceValidator, ActiveModel::Validations::ComparisonValidator, ActiveModel::Validations::ConfirmationValidator, ActiveModel::Validations::ExclusionValidator, ActiveModel::Validations::FormatValidator, ActiveModel::Validations::InclusionValidator, ActiveModel::Validations::LengthValidator, ActiveModel::Validations::NumericalityValidator, ActiveModel::Validations::PresenceValidator, ActiveModel::Validations::WithValidator, ActiveRecord::Validations::AbsenceValidator, ActiveRecord::Validations::AssociatedValidator, ActiveRecord::Validations::LengthValidator, ActiveRecord::Validations::NumericalityValidator, ActiveRecord::Validations::PresenceValidator, ActiveRecord::Validations::UniquenessValidator
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Validator
|
|
Instance Chain:
self,
Validator
|
|
Inherits: |
ActiveModel::Validator
|
Defined in: | activemodel/lib/active_model/validator.rb |
Overview
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.
Class Method Summary
-
.new(options) ⇒ EachValidator
constructor
Returns a new validator instance.
Validator
- Inherited
Instance Attribute Summary
Instance Method Summary
-
#check_validity!
Hook method that gets called by the initializer allowing verification that the arguments supplied are valid.
-
#validate(record)
Performs validation on the supplied record.
-
#validate_each(record, attribute, value)
Override this method in subclasses with the validation logic, adding errors to the records
errors
array where necessary.
Validator
- Inherited
Constructor Details
.new(options) ⇒ EachValidator
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
def initialize( ) @attributes = Array( .delete(:attributes)) raise ArgumentError, ":attributes cannot be blank" if @attributes.empty? super check_validity! end
Instance Attribute Details
#attributes (readonly)
[ GitHub ]# File 'activemodel/lib/active_model/validator.rb', line 135
attr_reader :attributes
Instance Method Details
#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 168
def check_validity! end
#validate(record)
Performs validation on the supplied record. By default this will call #validate_each to determine validity therefore subclasses should override #validate_each with validation logic.
# File 'activemodel/lib/active_model/validator.rb', line 150
def validate(record) attributes.each do |attribute| value = record.read_attribute_for_validation(attribute) next if (value.nil? && [:allow_nil]) || (value.blank? && [:allow_blank]) value = prepare_value_for_validation(value, record, attribute) validate_each(record, attribute, value) end 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 161
def validate_each(record, attribute, value) raise NotImplementedError, "Subclasses must implement a validate_each(record, attribute, value) method" end