Fix explicit names on multiple file fields
If a file field tag is passed the multiple option, it is turned into an array field (appending "[]"), but if the file field is passed an explicit name as an option, leave the name alone (do not append "[]"). Fixes #9830
This commit is contained in:
parent
d25e0c6f6a
commit
48dc5192ef
@ -1,5 +1,12 @@
|
|||||||
## Rails 4.0.0 (unreleased) ##
|
## Rails 4.0.0 (unreleased) ##
|
||||||
|
|
||||||
|
* Fix explicit names on multiple file fields. If a file field tag is passed
|
||||||
|
the multiple option, it is turned into an array field (appending `[]`),
|
||||||
|
but if the file field is passed an explicit name as an option, leave the
|
||||||
|
name alone (do not append `[]`). Fixes #9830
|
||||||
|
|
||||||
|
*Ryan McGeary*
|
||||||
|
|
||||||
* Add block support for the `mail_to` helper, similar to the `link_to` helper.
|
* Add block support for the `mail_to` helper, similar to the `link_to` helper.
|
||||||
|
|
||||||
*Sam Pohlenz*
|
*Sam Pohlenz*
|
||||||
|
@ -73,27 +73,26 @@ def add_default_name_and_id_for_value(tag_value, options)
|
|||||||
|
|
||||||
def add_default_name_and_id(options)
|
def add_default_name_and_id(options)
|
||||||
if options.has_key?("index")
|
if options.has_key?("index")
|
||||||
options["name"] ||= options.fetch("name"){ tag_name_with_index(options["index"]) }
|
options["name"] ||= options.fetch("name"){ tag_name_with_index(options["index"], options["multiple"]) }
|
||||||
options["id"] = options.fetch("id"){ tag_id_with_index(options["index"]) }
|
options["id"] = options.fetch("id"){ tag_id_with_index(options["index"]) }
|
||||||
options.delete("index")
|
options.delete("index")
|
||||||
elsif defined?(@auto_index)
|
elsif defined?(@auto_index)
|
||||||
options["name"] ||= options.fetch("name"){ tag_name_with_index(@auto_index) }
|
options["name"] ||= options.fetch("name"){ tag_name_with_index(@auto_index, options["multiple"]) }
|
||||||
options["id"] = options.fetch("id"){ tag_id_with_index(@auto_index) }
|
options["id"] = options.fetch("id"){ tag_id_with_index(@auto_index) }
|
||||||
else
|
else
|
||||||
options["name"] ||= options.fetch("name"){ tag_name }
|
options["name"] ||= options.fetch("name"){ tag_name(options["multiple"]) }
|
||||||
options["id"] = options.fetch("id"){ tag_id }
|
options["id"] = options.fetch("id"){ tag_id }
|
||||||
end
|
end
|
||||||
|
|
||||||
options["name"] += "[]" if options["multiple"] && !options["name"].ends_with?("[]")
|
|
||||||
options["id"] = [options.delete('namespace'), options["id"]].compact.join("_").presence
|
options["id"] = [options.delete('namespace'), options["id"]].compact.join("_").presence
|
||||||
end
|
end
|
||||||
|
|
||||||
def tag_name
|
def tag_name(multiple = false)
|
||||||
"#{@object_name}[#{sanitized_method_name}]"
|
"#{@object_name}[#{sanitized_method_name}]#{"[]" if multiple}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def tag_name_with_index(index)
|
def tag_name_with_index(index, multiple = false)
|
||||||
"#{@object_name}[#{index}][#{sanitized_method_name}]"
|
"#{@object_name}[#{index}][#{sanitized_method_name}]#{"[]" if multiple}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def tag_id
|
def tag_id
|
||||||
|
@ -361,6 +361,16 @@ def test_file_field_has_no_size
|
|||||||
assert_dom_equal expected, file_field("user", "avatar")
|
assert_dom_equal expected, file_field("user", "avatar")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_file_field_with_multiple_behavior
|
||||||
|
expected = '<input id="import_file" multiple="multiple" name="import[file][]" type="file" />'
|
||||||
|
assert_dom_equal expected, file_field("import", "file", :multiple => true)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_file_field_with_multiple_behavior_and_explicit_name
|
||||||
|
expected = '<input id="import_file" multiple="multiple" name="custom" type="file" />'
|
||||||
|
assert_dom_equal expected, file_field("import", "file", :multiple => true, :name => "custom")
|
||||||
|
end
|
||||||
|
|
||||||
def test_hidden_field
|
def test_hidden_field
|
||||||
assert_dom_equal(
|
assert_dom_equal(
|
||||||
'<input id="post_title" name="post[title]" type="hidden" value="Hello World" />',
|
'<input id="post_title" name="post[title]" type="hidden" value="Hello World" />',
|
||||||
|
Loading…
Reference in New Issue
Block a user