Class: DbMaximumLengthValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- DbMaximumLengthValidator
- Defined in:
- app/validators/db_maximum_length_validator.rb
Overview
Validates a field based on the maximum length of the underlying DB field, if there is one.
Instance Method Summary collapse
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
4 5 6 7 8 9 10 |
# File 'app/validators/db_maximum_length_validator.rb', line 4 def validate_each(record, attribute, value) limit = record.class.columns_hash[attribute.to_s].limit value = record[attribute.to_sym] if value && limit && value.to_s.length > limit record.errors.add(attribute.to_sym, :too_long, count: limit) end end |