8000 [Form] empty_value should not be selectable in choice field if the fi… · symfony/symfony@a9c27fb · GitHub
[go: up one dir, main page]

Skip to content

Commit a9c27fb

Browse files
committed
[Form] empty_value should not be selectable in choice field if the field is required
1 parent fefa9ed commit a9c27fb

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
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
@@ -69,7 +69,7 @@
6969
{% spaceless %}
7070
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
7171
{% if empty_value is not none %}
72-
<option value="">{{ empty_value|trans({}, translation_domain) }}</option>
72+
<option value="" disabled="disabled"{% if value is empty %} selected="selected"{% endif %}>{{ empty_value|trans({}, translation_domain) }}</option>
7373
{% endif %}
7474
{% if preferred_choices|length > 0 %}
7575
{% set options = preferred_choices %}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<?php echo $view['form']->block($form, 'widget_attributes') ?>
33
<?php if ($multiple): ?> multiple="multiple"<?php endif ?>
44
>
5-
<?php if (null !== $empty_value): ?><option value=""><?php echo $view->escape($view['translator']->trans($empty_value, array(), $translation_domain)) ?></option><?php endif; ?>
5+
<?php if (null !== $empty_value): ?><option value="" disabled="disabled"<?php if (empty($value) && "0" !== $value): ?> selected="selected"<?php endif ?>><?php echo $view->escape($view['translator']->trans($empty_value, array(), $translation_domain)) ?></option><?php endif; ?>
66
<?php if (count($preferred_choices) > 0): ?>
77
<?php echo $view['form']->block($form, 'choice_widget_options', array('choices' => $preferred_choices)) ?>
88
<?php if (count($choices) > 0 && null !== $separator): ?>

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ public function testSingleChoiceWithNonRequiredEmptyValue()
524524
[@name="name"]
525525
[not(@required)]
526526
[
527-
./option[@value=""][not(@selected)][.="[trans]Select&Anything&Not&Me[/trans]"]
527+
./option[@value=""][not(@selected)][@disabled][.="[trans]Select&Anything&Not&Me[/trans]"]
528528
/following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
529529
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
530530
]
@@ -548,7 +548,7 @@ public function testSingleChoiceRequiredWithEmptyValue()
548548
[@name="name"]
549549
[@required="required"]
550550
[
551-
./option[@value=""][.="[trans]Test&Me[/trans]"]
551+
./option[@value=""][not(@selected)][@disabled][.="[trans]Test&Me[/trans]"]
552552
/following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
553553
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
554554
]
@@ -571,7 +571,7 @@ public function testSingleChoiceRequiredWithEmptyValueViaView()
571571
[@name="name"]
572572
[@required="required"]
573573
[
574-
./option[@value=""][.="[trans][/trans]"]
574+
./option[@value=""][not(@selected)][@disabled][.="[trans][/trans]"]
575575
/following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
576576
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
577577
]
@@ -790,7 +790,7 @@ public function testCountryWithEmptyValue()
790790
$this->assertWidgetMatchesXpath($form->createView(), array(),
791791
'/select
792792
[@name="name"]
793-
[./option[@value=""][not(@selected)][.="[trans]Select&Country[/trans]"]]
793+
[./option[@value=""][not(@selected)][@disabled][.="[trans]Select&Country[/trans]"]]
794794
[./option[@value="AT"][@selected="selected"][.="[trans]Austria[/trans]"]]
795795
[count(./option)>201]
796796
'
@@ -852,13 +852,13 @@ public function testDateTimeWithEmptyValueGlobal()
852852
[
853853
./select
854854
[@id="name_date_month"]
855-
[./option[@value=""][.="[trans]Change&Me[/trans]"]]
855+
[./option[@value=""][@selected][@disabled][.="[trans]Change&Me[/trans]"]]
856856
/following-sibling::select
857857
[@id="name_date_day"]
858-
[./option[@value=""][.="[trans]Change&Me[/trans]"]]
858+
[./option[@value=""][@selected][@disabled][.="[trans]Change&Me[/trans]"]]
859859
/following-sibling::select
860860
[@id="name_date_year"]
861-
[./option[@value=""][.="[trans]Change&Me[/trans]"]]
861+
[./option[@value=""][@selected][@disabled][.="[trans]Change&Me[/trans]"]]
862862
]
863863
/following-sibling::div
864864
[@id="name_time"]
@@ -907,10 +907,10 @@ public function testDateTimeWithEmptyValueOnTime()
907907
[
908908
./select
909909
[@id="name_time_hour"]
910-
[./option[@value=""][.="[trans]Change&Me[/trans]"]]
910+
[./option[@value=""][not(@selected)][@disabled][.="[trans]Change&Me[/trans]"]]
911911
/following-sibling::select
912912
[@id="name_time_minute"]
913-
[./option[@value=""][.="[trans]Change&Me[/trans]"]]
913+
[./option[@value=""][not(@selected)][@disabled][.="[trans]Change&Me[/trans]"]]
914914
]
915915
]
916916
[count(.//select)=5]
@@ -1063,13 +1063,13 @@ public function testDateChoiceWithEmptyValueGlobal()
10631063
[
10641064
./select
10651065
[@id="name_month"]
1066-
[./option[@value=""][.="[trans]Change&Me[/trans]"]]
1066+
[./option[@value=""][@selected][@disabled][.="[trans]Change&Me[/trans]"]]
10671067
/following-sibling::select
10681068
[@id="name_day"]
1069-
[./option[@value=""][.="[trans]Change&Me[/trans]"]]
1069+
[./option[@value=""][@selected][@disabled][.="[trans]Change&Me[/trans]"]]
10701070
/following-sibling::select
10711071
[@id="name_year"]
1072-
[./option[@value=""][.="[trans]Change&Me[/trans]"]]
1072+
[./option[@value=""][@selected][@disabled][.="[trans]Change&Me[/trans]"]]
10731073
]
10741074
[count(./select)=3]
10751075
'
@@ -1096,7 +1096,7 @@ public function testDateChoiceWithEmptyValueOnYear()
10961096
[./option[@value="1"]]
10971097
/following-sibling::select
10981098
[@id="name_year"]
1099-
[./option[@value=""][.="[trans]Change&Me[/trans]"]]
1099+
[./option[@value=""][@selected][@disabled][.="[trans]Change&Me[/trans]"]]
11001100
]
11011101
[count(./select)=3]
11021102
'
@@ -1195,15 +1195,15 @@ public function testBirthDayWithEmptyValue()
11951195
[
11961196
./select
11971197
[@id="name_month"]
1198-
[./option[@value=""][.="[trans][/trans]"]]
1198+
[./option[@value=""][not(@selected)][@disabled][.="[trans][/trans]"]]
11991199
[./option[@value="1"][@selected="selected"]]
12001200
/following-sibling::select
12011201
[@id="name_day"]
1202-
[./option[@value=""][.="[trans][/trans]"]]
1202+
[./option[@value=""][not(@selected)][@disabled][.="[trans][/trans]"]]
12031203
[./option[@value="1"][@selected="selected"]]
12041204
/following-sibling::select
12051205
[@id="name_year"]
1206-
[./option[@value=""][.="[trans][/trans]"]]
1206+
[./option[@value=""][not(@selected)][@disabled][.="[trans][/trans]"]]
12071207
[./option[@value="1950"][@selected="selected"]]
12081208
]
12091209
[count(./select)=3]
@@ -1635,11 +1635,11 @@ public function testTimeWithEmptyValueGlobal()
16351635
[
16361636
./select
16371637
[@id="name_hour"]
1638-
[./option[@value=""][.="[trans]Change&Me[/trans]"]]
1638+
[./option[@value=""][@selected][@disabled][.="[trans]Change&Me[/trans]"]]
16391639
[count(./option)>24]
16401640
/following-sibling::select
16411641
[@id="name_minute"]
1642-
[./option[@value=""][.="[trans]Change&Me[/trans]"]]
1642+
[./option[@value=""][@selected][@disabled][.="[trans]Change&Me[/trans]"]]
16431643
[count(./option)>60]
16441644
]
16451645
[count(./select)=2]
@@ -1660,7 +1660,7 @@ public function testTimeWithEmptyValueOnYear()
16601660
[
16611661
./select
16621662
[@id="name_hour"]
1663-
[./option[@value=""][.="[trans]Change&Me[/trans]"]]
1663+
[./option[@value=""][@selected][@disabled][.="[trans]Change&Me[/trans]"]]
16641664
[count(./option)>24]
16651665
/following-sibling::select
16661666
[@id="name_minute"]
@@ -1710,7 +1710,7 @@ public function testTimezoneWithEmptyValue()
17101710

17111711
$this->assertWidgetMatchesXpath($form->createView(), array(),
17121712
'/select
1713-
[./option[@value=""][.="[trans]Select&Timezone[/trans]"]]
1713+
[./option[@value=""][@selected][@disabled][.="[trans]Select&Timezone[/trans]"]]
17141714
[count(./optgroup)>10]
17151715
[count(.//option)>201]
17161716
'

0 commit comments

Comments
 (0)
0