-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Form] ChoiceType: Fix cast to string from null choice #21160
Conversation
If I understood correctly (#17760 (comment)) then this PR don't affects the current behavior, because currently this already happen:
|
Well it shouldn't https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php#L207. |
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',
)) <select id="form_foo" name="form[foo]" required="required">
<option value="">Please choose</option>
<option value="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',
)) <select id="form_foo" name="form[foo]" required="required">
<option value="" selected="selected">Please choose</option>
<option value="red">Red</option>
<option value="" 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',
)) <select id="form_foo" name="form[foo]" required="required">
<option value selected="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',
)) <select id="form_foo" name="form[foo]" required="required">
<option value="" selected="selected">Please choose</option>
<option value="red">Red</option>
<option value="" 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? |
Maybe this result after: <select id="form_foo" name="form[foo]" required="required">
// placeholder option ignored because there is a choice with "" or null.
<option value selected="selected">None</option>
</select> should be the correct behavior and EDIT: Just as |
Otherwise, we need achieve render two options with the same empty value: <select id="form_foo" name="form[foo]">
<option value selected="selected">Please choose</option>
<option value>None</option>
</select> Is there the expected behavior? |
IMHO both results before your PR are expected: $builder->add('foo', 'choice', array(
'choices' => array('None' => null),
'placeholder' => 'Please choose',
)) <select id="form_foo" name="form[foo]" required="required">
<option value="">Please choose</option>
<option value="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',
)) <select id="form_foo" name="form[foo]" required="required">
<option value selected="selected">None</option> // missing placeholder and wrong selected
</select> The pre selection here is tricky but expected and the missing placeholder is expected too https://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 |
👍 Fair enough then :) |
Given the following:
Before:
After:
This results more consistent with expected behavior.