Skip to content

Commit acf7783

Browse files
committed
[Form] Use duplicate_preferred_choices to set value of ChoiceType
When the preferred choices are not duplicated an option has to be selected in the group of preferred choices. Closes #58561
1 parent 4d706f1 commit acf7783

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

Resources/views/Form/form_div_layout.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
{{- block('choice_widget_options') -}}
8686
</optgroup>
8787
{%- else -%}
88-
<option value="{{ choice.value }}"{% if choice.attr %}{% with { attr: choice.attr } %}{{ block('attributes') }}{% endwith %}{% endif %}{% if not render_preferred_choices|default(false) and choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice_translation_domain is same as(false) ? choice.label : choice.label|trans(choice.labelTranslationParameters, choice_translation_domain) }}</option>
88+
<option value="{{ choice.value }}"{% if choice.attr %}{% with { attr: choice.attr } %}{{ block('attributes') }}{% endwith %}{% endif %}{% if (not render_preferred_choices|default(false) or not (duplicate_preferred_choices ?? true)) and choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice_translation_domain is same as(false) ? choice.label : choice.label|trans(choice.labelTranslationParameters, choice_translation_domain) }}</option>
8989
{%- endif -%}
9090
{% endfor %}
9191
{%- endblock choice_widget_options -%}

Tests/Extension/AbstractDivLayoutTestCase.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,56 @@ public function testMultipleChoiceExpandedWithLabelsSetFalseByCallable()
856856
);
857857
}
858858

859+
public function testSingleChoiceWithoutDuplicatePreferredIsSelected()
860+
{
861+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&d', [
862+
'choices' => ['Choice&A' => '&a', 'Choice&B' => '&b', 'Choice&C' => '&c', 'Choice&D' => '&d'],
863+
'preferred_choices' => ['&b', '&d'],
864+
'duplicate_preferred_choices' => false,
865+
'multiple' => false,
866+
'expanded' => false,
867+
]);
868+
869+
$this->assertWidgetMatchesXpath($form->createView(), ['separator' => '-- sep --'],
870+
'/select
871+
[@name="name"]
872+
[
873+
./option[@value="&d"][@selected="selected"]
874+
/following-sibling::option[@disabled="disabled"][.="-- sep --"]
875+
/following-sibling::option[@value="&a"][not(@selected)]
876+
/following-sibling::option[@value="&c"][not(@selected)]
877+
]
878+
[count(./option)=5]
879+
'
880+
);
881+
}
882+
883+
public function testSingleChoiceWithoutDuplicateNotPreferredIsSelected()
884+
{
885+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&d', [
886+
'choices' => ['Choice&A' => '&a', 'Choice&B' => '&b', 'Choice&C' => '&c', 'Choice&D' => '&d'],
887+
'preferred_choices' => ['&b', '&d'],
888+
'duplicate_preferred_choices' => true,
889+
'multiple' => false,
890+
'expanded' => false,
891+
]);
892+
893+
$this->assertWidgetMatchesXpath($form->createView(), ['separator' => '-- sep --'],
894+
'/select
895+
[@name="name"]
896+
[
897+
./option[@value="&d"][not(@selected)]
898+
/following-sibling::option[@disabled="disabled"][.="-- sep --"]
899+
/following-sibling::option[@value="&a"][not(@selected)]
900+
/following-sibling::option[@value="&b"][not(@selected)]
901+
/following-sibling::option[@value="&c"][not(@selected)]
902+
/following-sibling::option[@value="&d"][@selected="selected"]
903+
]
904+
[count(./option)=7]
905+
'
906+
);
907+
}
908+
859909
public function testFormEndWithRest()
860910
{
861911
$view = $this->factory->createNamedBuilder('name', 'Symfony\Component\Form\Extension\Core\Type\FormType')

0 commit comments

Comments
 (0)