2020-03-09 14:55:53 +01:00
|
|
|
# frozen_string_literal: true
|
2015-09-27 13:51:13 +02:00
|
|
|
|
2020-03-09 14:55:53 +01:00
|
|
|
class ParseableDateValidator < ActiveModel::EachValidator
|
|
|
|
def validate_each(record, attribute, _value)
|
2015-09-27 13:51:13 +02:00
|
|
|
raw_value = record.read_attribute_before_type_cast(attribute)
|
|
|
|
return nil if raw_value.nil?
|
2020-03-09 14:55:53 +01:00
|
|
|
|
2018-03-18 21:21:59 +01:00
|
|
|
Date.parse(raw_value.to_json)
|
2015-09-27 13:51:13 +02:00
|
|
|
nil
|
2020-03-09 14:55:53 +01:00
|
|
|
rescue ArgumentError
|
|
|
|
record.errors[attribute] << (options[:message] || I18n.t('errors.messages.unparseable_date'))
|
2015-09-27 13:51:13 +02:00
|
|
|
end
|
|
|
|
end
|