diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md index 201e118971acba738607f9c6645f9f06d38eb91b..7b6008d5edb4876cb766538349a8c14e1ae59a57 100644 --- a/actionview/CHANGELOG.md +++ b/actionview/CHANGELOG.md @@ -1,3 +1,8 @@ +* Raise an ArgumentError when a false value for `include_blank` is passed to a + required select field (to comply with the HTML5 spec). + + *Grey Baker* + * Do not put partial name to `local_assigns` when rendering without an object or a collection. diff --git a/actionview/lib/action_view/helpers/tags/base.rb b/actionview/lib/action_view/helpers/tags/base.rb index acc6443a96f8f3c7fecc2d23fd1175f6326e067e..d57f26ba4feaf7c7e5a360b59551fd97a0ade24b 100644 --- a/actionview/lib/action_view/helpers/tags/base.rb +++ b/actionview/lib/action_view/helpers/tags/base.rb @@ -120,7 +120,12 @@ def sanitized_value(value) 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 @@ def select_content_tag(option_tags, options, html_options) 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) diff --git a/actionview/test/template/form_options_helper_test.rb b/actionview/test/template/form_options_helper_test.rb index d25fa3706fcd3557b8d0e277ed76b820e5d35c7d..d7daba8bf3c9a169f94eaf022400f07ec7d1b0fe 100644 --- a/actionview/test/template/form_options_helper_test.rb +++ b/actionview/test/template/form_options_helper_test.rb @@ -645,6 +645,13 @@ def test_select_with_blank ) end + def test_select_with_include_blank_false_and_required + @post = Post.new + @post.category = "" + e = assert_raises(ArgumentError) { select("post", "category", %w( abe hest), { include_blank: false }, required: 'required') } + assert_match(/include_blank cannot be false for a required field./, e.message) + end + def test_select_with_blank_as_string @post = Post.new @post.category = ""