2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-10-01 23:01:53 -05:00
|
|
|
class StrippedLengthValidator < ActiveModel::EachValidator
|
2013-06-13 03:18:17 -05:00
|
|
|
def self.validate(record, attribute, value, range)
|
2021-01-07 12:44:17 -06:00
|
|
|
if !value.nil?
|
|
|
|
html_comments_regexp = /<!--(.*?)-->/
|
|
|
|
stripped_length = value.gsub(html_comments_regexp, '')
|
|
|
|
stripped_length = stripped_length.strip.length
|
|
|
|
|
2013-06-13 03:18:17 -05:00
|
|
|
record.errors.add attribute, (I18n.t('errors.messages.too_short', count: range.begin)) unless
|
2013-02-15 19:58:33 -06:00
|
|
|
stripped_length >= range.begin
|
2014-07-04 01:38:27 -05:00
|
|
|
record.errors.add attribute, (I18n.t('errors.messages.too_long_validation', max: range.end, length: stripped_length)) unless
|
2013-02-15 19:58:33 -06:00
|
|
|
stripped_length <= range.end
|
|
|
|
else
|
2013-06-13 03:18:17 -05:00
|
|
|
record.errors.add attribute, (I18n.t('errors.messages.blank'))
|
2013-02-15 19:58:33 -06:00
|
|
|
end
|
|
|
|
end
|
2013-06-13 03:18:17 -05:00
|
|
|
|
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
# the `in` parameter might be a lambda when the range is dynamic
|
|
|
|
range = options[:in].lambda? ? options[:in].call : options[:in]
|
|
|
|
self.class.validate(record, attribute, value, range)
|
|
|
|
end
|
2013-02-15 19:58:33 -06:00
|
|
|
end
|