summaryrefslogtreecommitdiffstats
path: root/lib/redmine/core_ext
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2013-01-05 16:09:15 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2013-01-05 16:09:15 +0000
commit3e14c3017c037d93e562c3697cf50224dced7b50 (patch)
treed62163eba0051d645d1c7b17e55c434f05a737da /lib/redmine/core_ext
parent6ed7e091dfc833830ccd0e689714913c81ef6d22 (diff)
downloadredmine-3e14c3017c037d93e562c3697cf50224dced7b50.tar.gz
redmine-3e14c3017c037d93e562c3697cf50224dced7b50.zip
Adds a custom validator for dates (#12736).
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11124 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'lib/redmine/core_ext')
-rw-r--r--lib/redmine/core_ext/active_record.rb11
1 files changed, 11 insertions, 0 deletions
diff --git a/lib/redmine/core_ext/active_record.rb b/lib/redmine/core_ext/active_record.rb
index b18f95cd9..69bf76adc 100644
--- a/lib/redmine/core_ext/active_record.rb
+++ b/lib/redmine/core_ext/active_record.rb
@@ -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