stadtratmonitor/lib/parseable_date_validator.rb

13 lines
383 B
Ruby
Raw Normal View History

2015-09-27 13:51:13 +02:00
class ParseableDateValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
raw_value = record.read_attribute_before_type_cast(attribute)
return nil if raw_value.nil?
Date.parse(raw_value.to_json)
2015-09-27 13:51:13 +02:00
nil
rescue ArgumentError => e
record.errors[attribute] << (options[:message] || I18n.t("errors.messages.unparseable_date"))
end
end