Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a273e79

Browse filesBrowse files
committed
[Form] Fixed: "required" attribute is not added to <select> tag if no empty value
1 parent 33e9571 commit a273e79
Copy full SHA for a273e79

File tree

Expand file treeCollapse file tree

5 files changed

+28
-7
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+28
-7
lines changed

‎src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767

6868
{% block choice_widget_collapsed %}
6969
{% spaceless %}
70+
{% if required and empty_value is none and not empty_value_in_choices %}
71+
{% set required = false %}
72+
{% endif %}
7073
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
7174
{% if empty_value is not none %}
7275
<option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ empty_value|trans({}, translation_domain) }}</option>

‎src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget_collapsed.html.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget_collapsed.html.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<select
2-
<?php echo $view['form']->block($form, 'widget_attributes') ?>
2+
<?php echo $view['form']->block($form, 'widget_attributes', array(
3+
'required' => $required && (null !== $empty_value || $empty_value_in_choices)
4+
)) ?>
35
<?php if ($multiple): ?> multiple="multiple"<?php endif ?>
46
>
57
<?php if (null !== $empty_value): ?><option value=""<?php if ($required and empty($value) && "0" !== $value): ?> selected="selected"<?php endif?>><?php echo $view->escape($view['translator']->trans($empty_value, array(), $translation_domain)) ?></option><?php endif; ?>

‎src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ public function buildView(FormView $view, FormInterface $form, array $options)
119119
}
120120

121121
// Check if the choices already contain the empty value
122+
$view->vars['empty_value_in_choices'] = 0 !== count($options['choice_list']->getChoicesForValues(array('')));
123+
122124
// Only add the empty value option if this is not the case
123-
if (null !== $options['empty_value'] && 0 === count($options['choice_list']->getChoicesForValues(array('')))) {
125+
if (null !== $options['empty_value'] && !$view->vars['empty_value_in_choices']) {
124126
$view->vars['empty_value'] = $options['empty_value'];
125127
}
126128

‎src/Symfony/Component/Form/Tests/AbstractLayoutTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
+17-5Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,22 @@ public function testSingleChoice()
369369
'expanded' => false,
370370
));
371371

372+
// If the field is collapsed, has no "multiple" attribute, is required but
373+
// has *no* empty value, the "required" must not be added, otherwise
374+
// the resulting HTML is invalid.
375+
// https://github.com/symfony/symfony/issues/8942
376+
377+
// HTML 5 spec
378+
// http://www.w3.org/html/wg/drafts/html/master/forms.html#placeholder-label-option
379+
380+
// "If a select element has a required attribute specified, does not
381+
// have a multiple attribute specified, and has a display size of 1,
382+
// then the select element must have a placeholder label option."
383+
372384
$this->assertWidgetMatchesXpath($form->createView(), array(),
373385
'/select
374386
[@name="name"]
375-
[@required="required"]
387+
[not(@required)]
376388
[
377389
./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
378390
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
@@ -394,7 +406,7 @@ public function testSingleChoiceWithPreferred()
394406
$this->assertWidgetMatchesXpath($form->createView(), array('separator' => '-- sep --'),
395407
'/select
396408
[@name="name"]
397-
[@required="required"]
409+
[not(@required)]
398410
[
399411
./option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
400412
/following-sibling::option[@disabled="disabled"][not(@selected)][.="-- sep --"]
@@ -417,7 +429,7 @@ public function testSingleChoiceWithPreferredAndNoSeparator()
417429
$this->assertWidgetMatchesXpath($form->createView(), array('separator' => null),
418430
'/select
419431
[@name="name"]
420-
[@required="required"]
432+
[not(@required)]
421433
[
422434
./option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
423435
/following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
@@ -439,7 +451,7 @@ public function testSingleChoiceWithPreferredAndBlankSeparator()
439451
$this->assertWidgetMatchesXpath($form->createView(), array('separator' => ''),
440452
'/select
441453
[@name="name"]
442-
[@required="required"]
454+
[not(@required)]
443455
[
444456
./option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
445457
/following-sibling::option[@disabled="disabled"][not(@selected)][.=""]
@@ -1704,7 +1716,7 @@ public function testTimezone()
17041716
$this->assertWidgetMatchesXpath($form->createView(), array(),
17051717
'/select
17061718
[@name="name"]
1707-
[@required="required"]
1719+
[not(@required)]
17081720
[./optgroup
17091721
[@label="[trans]Europe[/trans]"]
17101722
[./option[@value="Europe/Vienna"][@selected="selected"][.="[trans]Vienna[/trans]"]]

‎src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,7 @@ public function testPassEmptyValueToView($multiple, $expanded, $required, $empty
10501050
$view = $form->createView();
10511051

10521052
$this->assertEquals($viewValue, $view->vars['empty_value']);
1053+
$this->assertFalse($view->vars['empty_value_in_choices']);
10531054
}
10541055

10551056
/**
@@ -1067,6 +1068,7 @@ public function testDontPassEmptyValueIfContainedInChoices($multiple, $expanded,
10671068
$view = $form->createView();
10681069

10691070
$this->assertNull($view->vars['empty_value']);
1071+
$this->assertTrue($view->vars['empty_value_in_choices']);
10701072
}
10711073

10721074
public function getOptionsWithEmptyValue()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.