From: Go MAEDA Date: Thu, 30 Mar 2023 01:11:19 +0000 (+0000) Subject: Limit the year to 4 digits in date input (#38231). X-Git-Tag: 5.1.0~198 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0bb664e43c7d1d0a5ba1ef661aada57a285ffd56;p=redmine.git Limit the year to 4 digits in date input (#38231). Patch by Go MAEDA. git-svn-id: https://svn.redmine.org/redmine/trunk@22161 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/config/initializers/10-patches.rb b/config/initializers/10-patches.rb index 0e01e28a5..4bca5edf4 100644 --- a/config/initializers/10-patches.rb +++ b/config/initializers/10-patches.rb @@ -82,6 +82,13 @@ module ActionView end end + module FormHelper + alias :date_field_without_max :date_field + def date_field(object_name, method, options = {}) + date_field_without_max(object_name, method, options.reverse_merge(max: '9999-12-31')) + end + end + module FormTagHelper alias :select_tag_without_non_empty_blank_option :select_tag def select_tag(name, option_tags = nil, options = {}) @@ -90,6 +97,11 @@ module ActionView end select_tag_without_non_empty_blank_option(name, option_tags, options) end + + alias :date_field_tag_without_max :date_field_tag + def date_field_tag(name, value = nil, options = {}) + date_field_tag_without_max(name, value, options.reverse_merge(max: '9999-12-31')) + end end module FormOptionsHelper diff --git a/test/unit/initializers/patches_test.rb b/test/unit/initializers/patches_test.rb index 317cbba14..57eff0662 100644 --- a/test/unit/initializers/patches_test.rb +++ b/test/unit/initializers/patches_test.rb @@ -21,6 +21,7 @@ require_relative '../../test_helper' class PatchesTest < ActiveSupport::TestCase include Redmine::I18n + include ActionView::Helpers::FormHelper def setup Setting.default_language = 'en' @@ -37,4 +38,14 @@ class PatchesTest < ActiveSupport::TestCase test "ActiveRecord::Base.human_attribute_name should default to humanized value if no translation has been found (useful for custom fields)" do assert_equal 'Patch name', ActiveRecord::Base.human_attribute_name('Patch name') end + + test 'ActionView::Helpers::FormHelper.date_field should add max=9999-12-31 to limit year value to 4 digits by default' do + assert_include 'max="9999-12-31"', date_field('issue', 'start_date') + assert_include 'max="2099-12-31"', date_field('issue', 'start_date', max: '2099-12-31') + end + + test 'ActionView::Helpers::FormTagHelper.date_field_tag should add max=9999-12-31 to limit year value to 4 digits by default' do + assert_include 'max="9999-12-31"', date_field_tag('start_date') + assert_include 'max="2099-12-31"', date_field_tag('issue', 'start_date', max: '2099-12-31') + end end