discourse/lib/validators/stripped_length_validator.rb
Navin 3fdba0019b Extract callbacks and validations for Post
Move Post create callbacks to PostCreate
Extract Post validations
Move stripped_length_validator to lib/validators
2013-06-09 20:47:04 +02:00

16 lines
747 B
Ruby

class StrippedLengthValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless value.nil?
stripped_length = value.strip.length
# the `in` parameter might be a lambda when the range is dynamic
range = options[:in].lambda? ? options[:in].call : options[:in]
record.errors.add attribute, (options[:message] || I18n.t('errors.messages.too_short', count: range.begin)) unless
stripped_length >= range.begin
record.errors.add attribute, (options[:message] || I18n.t('errors.messages.too_long', count: range.end)) unless
stripped_length <= range.end
else
record.errors.add attribute, (options[:message] || I18n.t('errors.messages.blank'))
end
end
end