diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-06-08 12:53:08 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-06-08 12:53:08 -0300 |
commit | a69e0a5fcfbcd76f259ab1ed290fafa8726b44ba (patch) | |
tree | 7372acefce4ae672f204c07f6282bd2916724aa0 /actionview/lib/action_view | |
parent | aeac00feb41ec99ef39b79cae31546ca875188d9 (diff) | |
parent | 077540738460d44c1463b3e52d2acb1fed9bc766 (diff) | |
download | rails-a69e0a5fcfbcd76f259ab1ed290fafa8726b44ba.tar.gz rails-a69e0a5fcfbcd76f259ab1ed290fafa8726b44ba.tar.bz2 rails-a69e0a5fcfbcd76f259ab1ed290fafa8726b44ba.zip |
Merge pull request #20124 from greysteil/fix-select-helper
Raise an ArgumentError when `include_blank` is false for a required select field
Diffstat (limited to 'actionview/lib/action_view')
-rw-r--r-- | actionview/lib/action_view/helpers/tags/base.rb | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/actionview/lib/action_view/helpers/tags/base.rb b/actionview/lib/action_view/helpers/tags/base.rb index acc6443a96..d57f26ba4f 100644 --- a/actionview/lib/action_view/helpers/tags/base.rb +++ b/actionview/lib/action_view/helpers/tags/base.rb @@ -120,7 +120,12 @@ module ActionView def select_content_tag(option_tags, options, html_options) html_options = html_options.stringify_keys add_default_name_and_id(html_options) - options[:include_blank] ||= true unless options[:prompt] || select_not_required?(html_options) + + if placeholder_required?(html_options) + raise ArgumentError, "include_blank cannot be false for a required field." if options[:include_blank] == false + options[:include_blank] ||= true unless options[:prompt] + end + value = options.fetch(:selected) { value(object) } select = content_tag("select", add_options(option_tags, options, value), html_options) @@ -131,8 +136,9 @@ module ActionView end end - def select_not_required?(html_options) - !html_options["required"] || html_options["multiple"] || html_options["size"].to_i > 1 + def placeholder_required?(html_options) + # See https://html.spec.whatwg.org/multipage/forms.html#attr-select-required + html_options["required"] && !html_options["multiple"] && html_options.fetch("size", 1).to_i == 1 end def add_options(option_tags, options, value = nil) |