Skip to content

[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

Merged
merged 1 commit into from
Feb 18, 2016

Conversation

Tobion
Copy link
Contributor

@Tobion Tobion commented Feb 13, 2016

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.
<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 replacing0 !== 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).

@Tobion
Copy link
Contributor Author

Tobion commented Feb 13, 2016

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

@Tobion Tobion force-pushed the fix-choice-placeholder-edge-cases branch 2 times, most recently from 7d34ccf to 2f8283e Compare February 13, 2016 05:30
@@ -528,6 +528,25 @@ public function testSingleChoice()
);
}

public function testSelectWithSizeBiggerThanOneCanBeRequired()
{
$form = $this->factory->createNamed('name', 'choice', '&a', array(
Copy link
Contributor Author

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

@Tobion Tobion force-pushed the fix-choice-placeholder-edge-cases branch from 2f8283e to 539bf46 Compare February 13, 2016 05:33
{
$form = $this->factory->createNamed('name', 'choice', '&a', array(
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
'choices_as_values' => true,
Copy link
Contributor Author

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

@Tobion Tobion force-pushed the fix-choice-placeholder-edge-cases branch from 539bf46 to 0efbc30 Compare February 13, 2016 12:17
@HeahDude
Copy link
Contributor

👍 Thanks for that PR.

What about the actual case where you got a placeholder and an empty string value standing for a null choice, then submitting the placeholder behaves like submitting null even if required is true ? Does this PR would help to fix the handling of placeholder discussed in #17760 if it's merged ?

@Tobion
Copy link
Contributor Author

Tobion commented Feb 17, 2016

This PR is rather independent of #17760.
If the final choices collection (with preferred choices logic in place) has as first item a choice with an empty value, then a placeholder is not added (like before). But if the empty choice is not the first choice, but comes later, then the placeholder is added anyway. In the backend it would just not be distinuishable between placeholder and an explicit empty choices, but that is expected if you do not have unique choices.

@HeahDude
Copy link
Contributor

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 null choice over the placeholder. And submitting the placeholder makes the submission fallback on the null choice.

Is that an expected behaviour in your opinion ?

@Tobion
Copy link
Contributor Author

Tobion commented Feb 17, 2016

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 null choice over the placeholder.

This makes sense when the data option is null (which it is by default basically). Since the initial data = null choice, it is selected. If 'data' => '' then the null choice should not be preselected anymore.

And submitting the placeholder makes the submission fallback on the null choice.

This is unexpected and not the way it should be which is think is caused because null and '' both map to the '' value with the changes done in #17760. This should be solved when null does not map to an empty string.

@fabpot
Copy link
Member

fabpot commented Feb 18, 2016

Thank you @Tobion.

@fabpot fabpot merged commit 0efbc30 into symfony:2.7 Feb 18, 2016
fabpot added a commit that referenced this pull request Feb 18, 2016
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
@Tobion Tobion deleted the fix-choice-placeholder-edge-cases branch February 18, 2016 12:37
HeahDude added a commit to HeahDude/symfony that referenced this pull request Feb 26, 2016
fabpot added a commit that referenced this pull request Feb 26, 2016
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
fabpot added a commit that referenced this pull request Feb 26, 2016
* 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
This was referenced Feb 28, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants