2013-02-15 19:58:33 -06:00
|
|
|
class StrippedLengthValidator < ActiveModel::EachValidator
|
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
unless value.nil?
|
|
|
|
stripped_length = value.strip.length
|
2013-04-10 07:54:10 -05: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 20:57:46 -06:00
|
|
|
record.errors.add attribute, (options[:message] || I18n.t('errors.messages.too_short', count: range.begin)) unless
|
2013-02-15 19:58:33 -06:00
|
|
|
stripped_length >= range.begin
|
2013-02-18 20:57:46 -06:00
|
|
|
record.errors.add attribute, (options[:message] || I18n.t('errors.messages.too_long', count: range.end)) unless
|
2013-02-15 19:58:33 -06:00
|
|
|
stripped_length <= range.end
|
|
|
|
else
|
2013-02-18 20:57:46 -06:00
|
|
|
record.errors.add attribute, (options[:message] || I18n.t('errors.messages.blank'))
|
2013-02-15 19:58:33 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|