Skip to content

Commit 49e0f50

Browse files
committed
[TwigBridge] Provide a default non-empty label on empty options to validate HTML5
1 parent 4d4c411 commit 49e0f50

File tree

5 files changed

+38
-20
lines changed

5 files changed

+38
-20
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
{%- endif -%}
6262
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
6363
{%- if placeholder is not none -%}
64-
<option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ placeholder != '' ? (translation_domain is same as(false) ? placeholder : placeholder|trans({}, translation_domain)) }}</option>
64+
<option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ placeholder != '' ? (translation_domain is same as(false) ? placeholder : placeholder|trans({}, translation_domain)) : '--' }}</option>
6565
{%- endif -%}
6666
{%- if preferred_choices|length > 0 -%}
6767
{% set options = preferred_choices %}

src/Symfony/Bridge/Twig/Resources/views/Form/foundation_5_layout.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@
156156
{%- endif -%}
157157
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple" data-customforms="disabled"{% endif %}>
158158
{% if placeholder is not none -%}
159-
<option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ translation_domain is same as(false) ? placeholder : placeholder|trans({}, translation_domain) }}</option>
159+
<option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ translation_domain is same as(false) ? placeholder : placeholder|trans({}, translation_domain) : '--' }}</option>
160160
{%- endif %}
161161
{%- if preferred_choices|length > 0 -%}
162162
{% set options = preferred_choices %}

src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap3LayoutTest.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,8 @@ public function testSingleChoiceWithPreferredAndNoSeparator()
626626
);
627627
}
628628

629+
// Note: blank separator produces non standard HTML (option text should not be empty),
630+
// but better not let Twig fix this automatically, user-side problem.
629631
public function testSingleChoiceWithPreferredAndBlankSeparator()
630632
{
631633
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', [
@@ -683,7 +685,7 @@ public function testSingleChoiceNonRequired()
683685
[@class="my&class form-control"]
684686
[not(@required)]
685687
[
686-
./option[@value=""][.=""]
688+
./option[@value=""][.="" or .="--"]
687689
/following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
688690
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
689691
]
@@ -707,7 +709,7 @@ public function testSingleChoiceNonRequiredNoneSelected()
707709
[@class="my&class form-control"]
708710
[not(@required)]
709711
[
710-
./option[@value=""][.=""]
712+
./option[@value=""][.="" or .="--"]
711713
/following-sibling::option[@value="&a"][not(@selected)][.="[trans]Choice&A[/trans]"]
712714
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
713715
]
@@ -766,6 +768,8 @@ public function testSingleChoiceRequiredWithPlaceholder()
766768
);
767769
}
768770

771+
// Note: blank placeholder produces non standard HTML (option text should not be empty),
772+
// but better not let Twig fix this automatically, user-side problem.
769773
public function testSingleChoiceRequiredWithPlaceholderViaView()
770774
{
771775
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', [
@@ -781,7 +785,7 @@ public function testSingleChoiceRequiredWithPlaceholderViaView()
781785
[@class="my&class form-control"]
782786
[@required="required"]
783787
[
784-
./option[@value=""][not(@selected)][not(@disabled)][.=""]
788+
./option[@value=""][not(@selected)][not(@disabled)][.="" or .="--"]
785789
/following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
786790
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
787791
]
@@ -1935,6 +1939,8 @@ public function testBirthDay()
19351939
);
19361940
}
19371941

1942+
// Note: blank placeholder produces non standard HTML (option text should not be empty),
1943+
// but better not let Twig fix this automatically, user-side problem.
19381944
public function testBirthDayWithPlaceholder()
19391945
{
19401946
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\BirthdayType', '1950-01-01', [
@@ -1950,17 +1956,17 @@ public function testBirthDayWithPlaceholder()
19501956
./select
19511957
[@id="name_month"]
19521958
[@class="form-control"]
1953-
[./option[@value=""][not(@selected)][not(@disabled)][.=""]]
1959+
[./option[@value=""][not(@selected)][not(@disabled)][.="" or .="--"]]
19541960
[./option[@value="1"][@selected="selected"]]
19551961
/following-sibling::select
19561962
[@id="name_day"]
19571963
[@class="form-control"]
1958-
[./option[@value=""][not(@selected)][not(@disabled)][.=""]]
1964+
[./option[@value=""][not(@selected)][not(@disabled)][.="" or .="--"]]
19591965
[./option[@value="1"][@selected="selected"]]
19601966
/following-sibling::select
19611967
[@id="name_year"]
19621968
[@class="form-control"]
1963-
[./option[@value=""][not(@selected)][not(@disabled)][.=""]]
1969+
[./option[@value=""][not(@selected)][not(@disabled)][.="" or .="--"]]
19641970
[./option[@value="1950"][@selected="selected"]]
19651971
]
19661972
[count(./select)=3]

src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap5LayoutTest.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,8 @@ public function testSingleChoiceWithPreferredAndNoSeparator()
610610
);
611611
}
612612

613+
// Note: blank separator produces non standard HTML (option text should not be empty),
614+
// but better not let Twig fix this automatically, user-side problem.
613615
public function testSingleChoiceWithPreferredAndBlankSeparator()
614616
{
615617
$form = $this->factory->createNamed('name', ChoiceType::class, '&a', [
@@ -667,7 +669,7 @@ public function testSingleChoiceNonRequired()
667669
[@class="my&class form-select"]
668670
[not(@required)]
669671
[
670-
./option[@value=""][.=""]
672+
./option[@value=""][.="" or .="--"]
671673
/following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
672674
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
673675
]
@@ -691,7 +693,7 @@ public function testSingleChoiceNonRequiredNoneSelected()
691693
[@class="my&class form-select"]
692694
[not(@required)]
693695
[
694-
./option[@value=""][.=""]
696+
./option[@value=""][.="" or .="--"]
695697
/following-sibling::option[@value="&a"][not(@selected)][.="[trans]Choice&A[/trans]"]
696698
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
697699
]
@@ -750,6 +752,8 @@ public function testSingleChoiceRequiredWithPlaceholder()
750752
);
751753
}
752754

755+
// Note: blank placeholder produces non standard HTML (option text should not be empty),
756+
// but better not let Twig fix this automatically, user-side problem.
753757
public function testSingleChoiceRequiredWithPlaceholderViaView()
754758
{
755759
$form = $this->factory->createNamed('name', ChoiceType::class, '&a', [
@@ -765,7 +769,7 @@ public function testSingleChoiceRequiredWithPlaceholderViaView()
765769
[@class="my&class form-select"]
766770
[@required="required"]
767771
[
768-
./option[@value=""][not(@selected)][not(@disabled)][.=""]
772+
./option[@value=""][not(@selected)][not(@disabled)][.="" or .="--"]
769773
/following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
770774
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
771775
]
@@ -1392,6 +1396,8 @@ public function testBirthDay()
13921396
);
13931397
}
13941398

1399+
// Note: blank placeholder produces non standard HTML (option text should not be empty),
1400+
// but better not let Twig fix this automatically, user-side problem.
13951401
public function testBirthDayWithPlaceholder()
13961402
{
13971403
$form = $this->factory->createNamed('name', BirthdayType::class, '1950-01-01', [
@@ -1410,17 +1416,17 @@ public function testBirthDayWithPlaceholder()
14101416
./select
14111417
[@id="name_month"]
14121418
[@class="form-select"]
1413-
[./option[@value=""][not(@selected)][not(@disabled)][.=""]]
1419+
[./option[@value=""][not(@selected)][not(@disabled)][.="" or .="--"]]
14141420
[./option[@value="1"][@selected="selected"]]
14151421
/following-sibling::select
14161422
[@id="name_day"]
14171423
[@class="form-select"]
1418-
[./option[@value=""][not(@selected)][not(@disabled)][.=""]]
1424+
[./option[@value=""][not(@selected)][not(@disabled)][.="" or .="--"]]
14191425
[./option[@value="1"][@selected="selected"]]
14201426
/following-sibling::select
14211427
[@id="name_year"]
14221428
[@class="form-select"]
1423-
[./option[@value=""][not(@selected)][not(@disabled)][.=""]]
1429+
[./option[@value=""][not(@selected)][not(@disabled)][.="" or .="--"]]
14241430
[./option[@value="1950"][@selected="selected"]]
14251431
]
14261432
[count(./select)=3]

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,8 @@ public function testSingleChoiceWithPreferredAndNoSeparator()
783783
);
784784
}
785785

786+
// Note: blank separator produces non standard HTML (option text should not be empty),
787+
// but better not let Twig fix this automatically, user-side problem.
786788
public function testSingleChoiceWithPreferredAndBlankSeparator()
787789
{
788790
$this->requiresFeatureSet(404);
@@ -841,7 +843,7 @@ public function testSingleChoiceNonRequired()
841843
[@name="name"]
842844
[not(@required)]
843845
[
844-
./option[@value=""][.=""]
846+
./option[@value=""][.="" or .="--"]
845847
/following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
846848
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
847849
]
@@ -864,7 +866,7 @@ public function testSingleChoiceNonRequiredNoneSelected()
864866
[@name="name"]
865867
[not(@required)]
866868
[
867-
./option[@value=""][.=""]
869+
./option[@value=""][.="" or .="--"]
868870
/following-sibling::option[@value="&a"][not(@selected)][.="[trans]Choice&A[/trans]"]
869871
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
870872
]
@@ -924,6 +926,8 @@ public function testSingleChoiceRequiredWithPlaceholder()
924926
);
925927
}
926928

929+
// Note: blank placeholder produces non standard HTML (option text should not be empty),
930+
// but better not let Twig fix this automatically, user-side problem.
927931
public function testSingleChoiceRequiredWithPlaceholderViaView()
928932
{
929933
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', [
@@ -941,7 +945,7 @@ public function testSingleChoiceRequiredWithPlaceholderViaView()
941945
[@name="name"]
942946
[@required="required"]
943947
[
944-
./option[@value=""][not(@selected)][not(@disabled)][.=""]
948+
./option[@value=""][not(@selected)][not(@disabled)][.="" or .="--"]
945949
/following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
946950
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
947951
]
@@ -1687,6 +1691,8 @@ public function testBirthDay()
16871691
);
16881692
}
16891693

1694+
// Note: blank placeholder produces non standard HTML (option text should not be empty),
1695+
// but better not let Twig fix this automatically, user-side problem.
16901696
public function testBirthDayWithPlaceholder()
16911697
{
16921698
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\BirthdayType', '1950-01-01', [
@@ -1700,15 +1706,15 @@ public function testBirthDayWithPlaceholder()
17001706
[
17011707
./select
17021708
[@id="name_month"]
1703-
[./option[@value=""][not(@selected)][not(@disabled)][.=""]]
1709+
[./option[@value=""][not(@selected)][not(@disabled)][.="" or .="--"]]
17041710
[./option[@value="1"][@selected="selected"]]
17051711
/following-sibling::select
17061712
[@id="name_day"]
1707-
[./option[@value=""][not(@selected)][not(@disabled)][.=""]]
1713+
[./option[@value=""][not(@selected)][not(@disabled)][.="" or .="--"]]
17081714
[./option[@value="1"][@selected="selected"]]
17091715
/following-sibling::select
17101716
[@id="name_year"]
1711-
[./option[@value=""][not(@selected)][not(@disabled)][.=""]]
1717+
[./option[@value=""][not(@selected)][not(@disabled)][.="" or .="--"]]
17121718
[./option[@value="1950"][@selected="selected"]]
17131719
]
17141720
[count(./select)=3]

0 commit comments

Comments
 (0)