-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Fix choice placeholder edge cases #17787
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
Conversation
In theory these fixes could be done in 2.3 but would cause merging pain with the renaming of empty_value to placeholder. Not sure if it's worth. In 2.8 the small template adjustments also need to be made to foundation_5_layout.html.twig |
7d34ccf
to
2f8283e
Compare
@@ -528,6 +528,25 @@ public function testSingleChoice() | |||
); | |||
} | |||
|
|||
public function testSelectWithSizeBiggerThanOneCanBeRequired() | |||
{ | |||
$form = $this->factory->createNamed('name', 'choice', '&a', array( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remember 'Symfony\Component\Form\Extension\Core\Type\ChoiceType'
must be used in 2.8
2f8283e
to
539bf46
Compare
{ | ||
$form = $this->factory->createNamed('name', 'choice', '&a', array( | ||
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), | ||
'choices_as_values' => true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be removed in 3.0 or 3.1
539bf46
to
0efbc30
Compare
👍 Thanks for that PR. What about the actual case where you got a |
This PR is rather independent of #17760. |
Thanks for this precision. But it seems that the last issue with the placeholder in #17760 (not totally related though) is that the pre selection is done on the Is that an expected behaviour in your opinion ? |
This makes sense when the
This is unexpected and not the way it should be which is think is caused because |
Thank you @Tobion. |
This PR was merged into the 2.7 branch. Discussion ---------- [Form] Fix choice placeholder edge cases | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Fixing several problems with choice placeholder that enhances #9030 for more edge cases: - A choice with an empty value manually added in the choices array should only be considered a placeholder when it is the first element in the final choice select. This is part of the HTML spec and how browsers also behave. If you select a choice with an empty value that is not the first option, it will still pass the "required" check and thus submit the empty value. So it's not a placeholder. If in the example below you move the empty option to the first place, the browsers will error on submit that you must select a value. So only then it is a placeholder to show as initial value. ```html <select id="form_timezone" name="form[timezone]" required="required"> <option value="Africa/Abidjan">Abidjan</option> <option value="">Empty</option> </select> ``` Also the validator https://validator.w3.org/nu/ will mark the above code as error: > The first child option element of a select element with a required attribute, and without a multiple attribute, and without a size attribute whose value is greater than 1, must have either an empty value attribute, or must have no text content. Consider either adding a placeholder option label, or adding a size attribute with a value equal to the number of option elements. This is fixed by replacing`0 !== count($choiceList->getChoicesForValues(array('')))` with `$view->vars['placeholder_in_choices'] = $choiceListView->hasPlaceholder()`. Which means, the required attribute is removed automatically because the select form element is required implicitly anyway due to the nature of the choice UI. - As the above quote mentions, the `size` attribute also has impact. Namely for a select with size > 1 it can be possible to have a required attribute even without placeholder. This is because when the size > 1, there is no default choice selected (similar to select with "multiple"). - A placeholder for required radio buttons or a select with size > 1 does not make sense as it would just be fake data that can be submitted (similar to the ignored placeholder for multi-select and checkboxes). Commits ------- 0efbc30 [Form] fix edge cases with choice placeholder
This PR was merged into the 3.0 branch. Discussion ---------- [3.0] [Form/Tests] minor fix following #17787 | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | - Commits ------- 020ac04 [3.0] [Tests] minor fix following #17787
* 3.0: [3.0] [Tests] minor fix following #17787 [2.8] [Form] minor fix some tests with placeholder in AbstractLayout [DependencyInjection] fix tests Validate XLIFF translation files [DependencyInjection] replace alias in factories replace alias in factory services
Fixing several problems with choice placeholder that enhances #9030 for more edge cases:
This is part of the HTML spec and how browsers also behave. If you select a choice with an empty value that is not the first option, it will still pass the "required" check
and thus submit the empty value. So it's not a placeholder.
If in the example below you move the empty option to the first place, the browsers will error on submit that you
must select a value. So only then it is a placeholder to show as initial value.
Also the validator https://validator.w3.org/nu/ will mark the above code as error:
This is fixed by replacing
0 !== count($choiceList->getChoicesForValues(array('')))
with$view->vars['placeholder_in_choices'] = $choiceListView->hasPlaceholder()
.Which means, the required attribute is removed automatically because the select form element is required implicitly anyway due to the nature of the choice UI.
size
attribute also has impact. Namely for a select with size > 1 it can be possible to have a required attribute even without placeholder.This is because when the size > 1, there is no default choice selected (similar to select with "multiple").