aboutsummaryrefslogtreecommitdiffstats
path: root/actionview/lib/action_view/helpers/tags/base.rb
diff options
context:
space:
mode:
authorGrey Baker <greysteil@gmail.com>2015-05-12 15:10:20 +0100
committerGrey Baker <greysteil@gmail.com>2015-06-08 16:41:44 +0100
commit077540738460d44c1463b3e52d2acb1fed9bc766 (patch)
tree7372acefce4ae672f204c07f6282bd2916724aa0 /actionview/lib/action_view/helpers/tags/base.rb
parentaeac00feb41ec99ef39b79cae31546ca875188d9 (diff)
downloadrails-077540738460d44c1463b3e52d2acb1fed9bc766.tar.gz
rails-077540738460d44c1463b3e52d2acb1fed9bc766.tar.bz2
rails-077540738460d44c1463b3e52d2acb1fed9bc766.zip
Raise an ArgumentError when `include_blank` is false for a required field in
`Tags::Base#select_content_tag`. Previously, passing a falsey value to `include_blank` would be ignored if the field was required, and a blank line would still be inserted. The following will now raise instead of quietly failing: `select("post", "category", %w(a required field), { include_blank: false }, required: 'required')`
Diffstat (limited to 'actionview/lib/action_view/helpers/tags/base.rb')
-rw-r--r--actionview/lib/action_view/helpers/tags/base.rb12
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)