diff options
author | Marius Balteanu <marius.balteanu@zitec.com> | 2021-10-24 09:57:56 +0000 |
---|---|---|
committer | Marius Balteanu <marius.balteanu@zitec.com> | 2021-10-24 09:57:56 +0000 |
commit | dcf69e509a9342f1c493303a8b0a4bee278e00b0 (patch) | |
tree | 4557406fdd7c67623889ded97d30647252ae5bad /app/validators/date_validator.rb | |
parent | 9cb4a5f4ca4a808a90d8f29c06dbb05e8d95aa91 (diff) | |
download | redmine-dcf69e509a9342f1c493303a8b0a4bee278e00b0.tar.gz redmine-dcf69e509a9342f1c493303a8b0a4bee278e00b0.zip |
Move custom DateValidator from @lib/redmine/core_ext/active_record.rb@ to @app/validators/date_validator.rb@ (#29914, #32938).
git-svn-id: http://svn.redmine.org/redmine/trunk@21258 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/validators/date_validator.rb')
-rw-r--r-- | app/validators/date_validator.rb | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/app/validators/date_validator.rb b/app/validators/date_validator.rb new file mode 100644 index 000000000..df366b04f --- /dev/null +++ b/app/validators/date_validator.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +# Redmine - project management software +# Copyright (C) 2006-2021 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +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 /\A\d{4}-\d{2}-\d{2}( 00:00:00)?\z/.match?(before_type_cast) && value + record.errors.add attribute, :not_a_date + end + end + end +end |