2013-02-15 20:58:33 -05:00
|
|
|
class StrippedLengthValidator < ActiveModel::EachValidator
|
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
unless value.nil?
|
|
|
|
stripped_length = value.strip.length
|
2013-04-10 08:54:10 -04:00
|
|
|
# the `in` parameter might be a lambda when the range is dynamic
|
|
|
|
range = options[:in].lambda? ? options[:in].call : options[:in]
|
2013-02-18 21:57:46 -05:00
|
|
|
record.errors.add attribute, (options[:message] || I18n.t('errors.messages.too_short', count: range.begin)) unless
|
2013-02-15 20:58:33 -05:00
|
|
|
stripped_length >= range.begin
|
2013-02-18 21:57:46 -05:00
|
|
|
record.errors.add attribute, (options[:message] || I18n.t('errors.messages.too_long', count: range.end)) unless
|
2013-02-15 20:58:33 -05:00
|
|
|
stripped_length <= range.end
|
|
|
|
else
|
2013-02-18 21:57:46 -05:00
|
|
|
record.errors.add attribute, (options[:message] || I18n.t('errors.messages.blank'))
|
2013-02-15 20:58:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|