Class: ActiveModel::EachValidator
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.
Instance Attribute Summary (collapse)
-
- (Object) attributes
readonly
Returns the value of attribute attributes.
Attributes inherited from Validator
Instance Method Summary (collapse)
-
- (Object) check_validity!
Hook method that gets called by the initializer allowing verification that the arguments supplied are valid.
-
- (EachValidator) initialize(options)
constructor
Returns a new validator instance.
-
- (Object) validate(record)
Performs validation on the supplied record.
-
- (Object) validate_each(record, attribute, value)
Override this method in subclasses with the validation logic, adding errors to the records errors array where necessary.
Methods inherited from Validator
Constructor Details
- (EachValidator) initialize(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.
139 140 141 142 143 144 |
# File 'activemodel/lib/active_model/validator.rb', line 139 def initialize() @attributes = Array.wrap(.delete(:attributes)) raise ":attributes cannot be blank" if @attributes.empty? super check_validity! end |
Instance Attribute Details
- (Object) attributes (readonly)
Returns the value of attribute attributes
134 135 136 |
# File 'activemodel/lib/active_model/validator.rb', line 134 def attributes @attributes end |
Instance Method Details
- (Object) 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.
166 167 |
# File 'activemodel/lib/active_model/validator.rb', line 166 def check_validity! end |
- (Object) 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.
149 150 151 152 153 154 155 |
# File 'activemodel/lib/active_model/validator.rb', line 149 def validate(record) attributes.each do |attribute| value = record.read_attribute_for_validation(attribute) next if (value.nil? && [:allow_nil]) || (value.blank? && [:allow_blank]) validate_each(record, attribute, value) end end |
- (Object) validate_each(record, attribute, value)
Override this method in subclasses with the validation logic, adding errors to the records errors array where necessary.
159 160 161 |
# File 'activemodel/lib/active_model/validator.rb', line 159 def validate_each(record, attribute, value) raise NotImplementedError, "Subclasses must implement a validate_each(record, attribute, value) method" end |