Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Fixing select[multiple] html specification problem. #1552

Merged
merged 1 commit into from Jun 11, 2011

Conversation

Projects
None yet
6 participants
Contributor

bogdan commented Jun 8, 2011

There is very good work around for deselected check box in FormHelper#check_box.

I think it's good idea to use same work around for select[multiple] that have the same problem:

Server do not receive any value when all options got deselected.

So we will generate hidden input with the same name and blank value before each multiple select.

Fixing select[multiple] html specification problem.
Generating hidden input with same name before each multiple select

drogus added a commit that referenced this pull request Jun 11, 2011

Merge pull request #1552 from bogdan/select
Fixing select[multiple] html specification problem.

@drogus drogus merged commit f5e1548 into rails:master Jun 11, 2011

drogus added a commit that referenced this pull request Jun 11, 2011

jake3030 pushed a commit to jake3030/rails that referenced this pull request Jun 28, 2011

Squash memory leak when calling flush with an empty buffer
[#1552 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>

@denis denis commented on the diff Jan 2, 2013

actionpack/test/template/form_options_helper_test.rb
@@ -457,6 +457,22 @@ def test_select_under_fields_for_with_string_and_given_prompt
)
end
+ def test_select_with_multiple_to_add_hidden_input
+ output_buffer = select(:post, :category, "", {}, :multiple => true)
+ assert_dom_equal(
+ "<input type=\"hidden\" name=\"post[category][]\" value=\"\"/><select multiple=\"multiple\" id=\"post_category\" name=\"post[category][]\"></select>",
@denis

denis Jan 2, 2013

@bogdan it looks like the value from the hidden field will not be overwritten since the field name contains [] in the end. So for the case when nothing selected it will pass:

{post: {category: [""]}}

and for the case when something was selected:

{post: {category: ["", "Category 1", "Category 2"]}}
@bogdan

bogdan Jan 2, 2013

Contributor

it works as you said for nothing. As this patch got accepted: this behavior is expected, but it is not perfect as we expect to pass {post: {category: []}}, but I don't know how to do that.

The case when something is selected: Are you sure?

I would recommend the following workaround anyway: http://stackoverflow.com/questions/8929230/why-is-the-first-element-always-blank-in-my-rails-multi-select-using-an-embedde/8939205#8939205

taelor commented Mar 5, 2013

The case when something is selected: Are you sure?

I am sure, I am also seeing this, and its actually kind of annoying. I'd rather not have to clean up params in my controller because of Rails magic silently adding things. I am not even using this for updating a model, but I do use it when trying to query against a list of users belonging to different organizations. This is what comes over when you have one or more selected:

"organization_ids"=>["", "66284513", "676685643"]

While I would prefer not to have the hidden field generated at all, is there anyway we could throw an option to not have the hidden field generate?

same problem here

There, same problem too

senny added a commit that referenced this pull request Jul 17, 2014

docs, `select` and friends with `multiple=true` include a blank string.
[Jonas Baumann & Yves Senn]

The submitted params from a select with `multiple: true` look as follows:

```
{post: {category: [""]}}

{post: {category: ["", "Category 1", "Category 2"]}}
```

This is a follow up to #1552.

senny added a commit that referenced this pull request Jul 17, 2014

docs, `select` and friends with `multiple=true` include a blank string.
[Jonas Baumann & Yves Senn]

The submitted params from a select with `multiple: true` look as follows:

```
{post: {category: [""]}}

{post: {category: ["", "Category 1", "Category 2"]}}
```

This is a follow up to #1552.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment