1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #32087 from prikha/add-better-select-tag-option-handling

Let select render default selected option for required field
This commit is contained in:
Rafael França 2018-02-27 22:52:54 -05:00 committed by GitHub
commit a14a6b8834
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 1 deletions

View file

@ -1,5 +1,26 @@
## Rails 6.0.0.alpha (Unreleased) ##
* Enable select tag helper to mark `prompt` option as `selected` and/or `disabled` for `required`
field. Example:
select :post,
:category,
["lifestyle", "programming", "spiritual"],
{ selected: "", disabled: "", prompt: "Choose one" },
{ required: true }
Placeholder option would be selected and disabled. The HTML produced:
<select required="required" name="post[category]" id="post_category">
<option disabled="disabled" selected="selected" value="">Choose one</option>
<option value="lifestyle">lifestyle</option>
<option value="programming">programming</option>
<option value="spiritual">spiritual</option></select>
For details see GH#32080
*Sergey Prikhodko*
* Don't enforce UTF-8 by default
With the disabling of TLS 1.0 by most major websites, continuing to run

View file

@ -170,7 +170,11 @@ module ActionView
option_tags = tag_builder.content_tag_string("option", options[:include_blank].kind_of?(String) ? options[:include_blank] : nil, value: "") + "\n" + option_tags
end
if value.blank? && options[:prompt]
option_tags = tag_builder.content_tag_string("option", prompt_text(options[:prompt]), value: "") + "\n" + option_tags
tag_options = { value: "" }.tap do |prompt_opts|
prompt_opts[:disabled] = true if options[:disabled] == ""
prompt_opts[:selected] = true if options[:selected] == ""
end
option_tags = tag_builder.content_tag_string("option", prompt_text(options[:prompt]), tag_options) + "\n" + option_tags
end
option_tags
end

View file

@ -511,6 +511,16 @@ class FormOptionsHelperTest < ActionView::TestCase
)
end
def test_required_select_with_default_and_selected_placeholder
assert_dom_equal(
['<select required="required" name="post[category]" id="post_category"><option disabled="disabled" selected="selected" value="">Choose one</option>',
'<option value="lifestyle">lifestyle</option>',
'<option value="programming">programming</option>',
'<option value="spiritual">spiritual</option></select>'].join("\n"),
select(:post, :category, ["lifestyle", "programming", "spiritual"], { selected: "", disabled: "", prompt: "Choose one" }, { required: true })
)
end
def test_select_with_grouped_collection_as_nested_array
@post = Post.new