Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[Form] ChoiceType: Fix cast to string from null choice#21160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
HeahDude commentedJan 4, 2017
yceruto commentedJan 4, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
If I understood correctly (#17760 (comment)) then this PR don't affects the current behavior, because currently this already happen:
|
HeahDude commentedJan 4, 2017
Well it shouldn'thttps://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php#L207. |
yceruto commentedJan 4, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Agreed, but IMHO this PR is not the source of bad behavior, this already happen before that: Before this PR: Result (should show two options and placeholder selected): $builder->add('foo','choice',array('choices' =>array('None' =>null),'placeholder' =>'Please choose',)) <selectid="form_foo"name="form[foo]"required="required"><optionvalue="">Please choose</option><optionvalue="0"selected="selected">None</option> // wrong selected</select> Result (should show three options and placeholder selected): $builder->add('foo','choice',array('choices' =>array('Red' =>'red','None' =>''),'placeholder' =>'Please choose',)) <selectid="form_foo"name="form[foo]"required="required"><optionvalue=""selected="selected">Please choose</option><optionvalue="red">Red</option><optionvalue=""selected="selected">None</option> // wrong selected</select> After this PR: Result (should show two options and placeholder selected): $builder->add('foo','choice',array('choices' =>array('None' =>null),// or array('None' => ''),'placeholder' =>'Please choose',)) <selectid="form_foo"name="form[foo]"required="required"><optionvalueselected="selected">None</option></select> Result (should show three options and placeholder selected): $builder->add('foo','choice',array('choices' =>array('Red' =>'red','None' =>null),// or 'None' => '''placeholder' =>'Please choose',)) <selectid="form_foo"name="form[foo]"required="required"><optionvalue=""selected="selected">Please choose</option><optionvalue="red">Red</option><optionvalue=""selected="selected">None</option> // wrong selected</select> The result in all cases (before/after) is wrong in any way. This PR resolves an inconsistency issue, but do you think that makes the problem worse? |
yceruto commentedJan 4, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Maybe this result after: <selectid="form_foo"name="form[foo]"required="required"> // placeholder option ignored because there is a choice with "" or null.<optionvalueselected="selected">None</option></select> should be the correct behavior and EDIT: Just as |
yceruto commentedJan 4, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Otherwise, we need achieve render two options with the same empty value: <selectid="form_foo"name="form[foo]"><optionvalueselected="selected">Please choose</option><optionvalue>None</option></select> Is there the expected behavior? |
HeahDude commentedJan 4, 2017
IMHO both results before your PR are expected: $builder->add('foo','choice',array('choices' =>array('None' =>null),'placeholder' =>'Please choose',)) <selectid="form_foo"name="form[foo]"required="required"><optionvalue="">Please choose</option><optionvalue="0"selected="selected">None</option> // wrong selected</select> I don't think this is wrongly selected if the data passed on form creation is $builder->add('foo','choice',array('choices' =>array('None' =>''),'placeholder' =>'Please choose',)) <selectid="form_foo"name="form[foo]"required="required"><optionvalueselected="selected">None</option> // missing placeholder and wrong selected</select> The pre selection here is tricky but expected and the missing placeholder is expected toohttps://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/ChoiceList/View/ChoiceListView.php#L55. The standard is about having the placeholder first because for a select input, the first value is always selected. By default, the empty string in this context means "I haven't choose anything yet", and it makes sense in some cases to bind it to an empty string or null in the model. The thing is that it does not make sense to use an empty string as model choice (which may not be the first) and use the placeholder option at the same time, because even in this edge case the If we want more control on it, we should consider it a feature and maybe add a |
yceruto commentedJan 4, 2017
👍 Fair enough then :) |
Uh oh!
There was an error while loading.Please reload this page.
Given the following:
Before:
After:
This results more consistent with expected behavior.