Module: Hydra::Validations::EnumerableBehavior
- Included in:
- FormatValidator, InclusionValidator
- Defined in:
- lib/hydra/validations/enumerable_behavior.rb
Overview
EnumerableBehavior - a mixin for subclasses of ActiveModel::EachValidator adding validation for each member of an enumerable attribute value.
Behavior includes ‘fixing’ each error message to include the specific value which was invalid.
allow_empty option can be used instead of allow_blank for greater clarity and precision with enumerable values. If a validator includes this mixin, then an empty enumerable value will always be invalid if allow_empty is false or nil.
Instance Method Summary collapse
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/hydra/validations/enumerable_behavior.rb', line 17 def validate_each(record, attribute, value) return super unless value.respond_to?(:each) if value.respond_to?(:empty?) && value.empty? record.errors.add(attribute, "can't be empty") unless [:allow_empty] else value.each do |v| prev = record.errors[attribute].size rescue 0 super(record, attribute, v) = record.errors[attribute] if && .size > prev record.errors.add(attribute, (v, .pop)) end end end end |