Class: DynamicLengthValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- DynamicLengthValidator
- Defined in:
- app/validators/dynamic_length_validator.rb
Overview
Validates that value of attribute on record is meets the
record.dynamic_length_validation_options(attribute).
Works similar to LengthValidator, but the minimum and maximum lengths are controlled dynamically by the record to
allow the validation to vary based on some other attribute in the record.
Instance Method Summary collapse
-
#validate_each(record, attribute, value) ⇒ void
Validates that value's
#lengthmeets the minimum and maximum options inrecord.dynamic_length_validation_options(attribute).
Instance Method Details
#validate_each(record, attribute, value) ⇒ void
This method returns an undefined value.
Validates that value's #length meets the minimum and maximum options in
record.dynamic_length_validation_options(attribute).
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/validators/dynamic_length_validator.rb', line 15 def validate_each(record, attribute, value) # dynamic_options are not checked for validity the way options would be for LengthValidator because # dynamic_length_validation_options can be {} in some cases. = record.(attribute) value_length = value.length ActiveModel::Validations::LengthValidator::CHECKS.each do |key, validity_check| check_value = [key] if check_value unless value_length.send(validity_check, check_value) = .except(*ActiveModel::Validations::LengthValidator::RESERVED_OPTIONS) [:count] = check_value = [:message] = ActiveModel::Validations::LengthValidator::MESSAGES[key] unless = [] if [:message] = end end record.errors.add(attribute, , ) end end end end |