Adds a custom validator for dates (#12736).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11124 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang
2013-01-05 16:09:15 +00:00
parent 6ed7e091df
commit 3e14c3017c
4 changed files with 16 additions and 16 deletions
+11
View File
@@ -38,3 +38,14 @@ module ActiveRecord
end
end
end
class DateValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
before_type_cast = record.attributes_before_type_cast[attribute.to_s]
if before_type_cast.is_a?(String) && before_type_cast.present?
unless before_type_cast =~ /\A\d{4}-\d{2}-\d{2}\z/ && value
record.errors.add attribute, :not_a_date
end
end
end
end