Skip to content

Commit b37086d

Browse files
committed
Merge branch '7.2' into 7.3
* 7.2: [Form] Use duplicate_preferred_choices to set value of ChoiceType
2 parents 38e0df1 + 62ecae9 commit b37086d

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
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
@@ -89,7 +89,7 @@
8989
{{- block('choice_widget_options') -}}
9090
</optgroup>
9191
{%- else -%}
92-
<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>
92+
<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>
9393
{%- endif -%}
9494
{% endfor %}
9595
{%- endblock choice_widget_options -%}

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,56 @@ public function testMultipleChoiceExpandedWithLabelsSetFalseByCallable()
870870
);
871871
}
872872

873+
public function testSingleChoiceWithoutDuplicatePreferredIsSelected()
874+
{
875+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&d', [
876+
'choices' => ['Choice&A' => '&a', 'Choice&B' => '&b', 'Choice&C' => '&c', 'Choice&D' => '&d'],
877+
'preferred_choices' => ['&b', '&d'],
878+
'duplicate_preferred_choices' => false,
879+
'multiple' => false,
880+
'expanded' => false,
881+
]);
882+
883+
$this->assertWidgetMatchesXpath($form->createView(), ['separator' => '-- sep --'],
884+
'/select
885+
[@name="name"]
886+
[
887+
./option[@value="&d"][@selected="selected"]
888+
/following-sibling::option[@disabled="disabled"][.="-- sep --"]
889+
/following-sibling::option[@value="&a"][not(@selected)]
890+
/following-sibling::option[@value="&c"][not(@selected)]
891+
]
892+
[count(./option)=5]
893+
'
894+
);
895+
}
896+
897+
public function testSingleChoiceWithoutDuplicateNotPreferredIsSelected()
898+
{
899+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&d', [
900+
'choices' => ['Choice&A' => '&a', 'Choice&B' => '&b', 'Choice&C' => '&c', 'Choice&D' => '&d'],
901+
'preferred_choices' => ['&b', '&d'],
902+
'duplicate_preferred_choices' => true,
903+
'multiple' => false,
904+
'expanded' => false,
905+
]);
906+
907+
$this->assertWidgetMatchesXpath($form->createView(), ['separator' => '-- sep --'],
908+
'/select
909+
[@name="name"]
910+
[
911+
./option[@value="&d"][not(@selected)]
912+
/following-sibling::option[@disabled="disabled"][.="-- sep --"]
913+
/following-sibling::option[@value="&a"][not(@selected)]
914+
/following-sibling::option[@value="&b"][not(@selected)]
915+
/following-sibling::option[@value="&c"][not(@selected)]
916+
/following-sibling::option[@value="&d"][@selected="selected"]
917+
]
918+
[count(./option)=7]
919+
'
920+
);
921+
}
922+
873923
public function testFormEndWithRest()
874924
{
875925
$view = $this->factory->createNamedBuilder('name', 'Symfony\Component\Form\Extension\Core\Type\FormType')

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ public function buildView(FormView $view, FormInterface $form, array $options):
275275

276276
public function finishView(FormView $view, FormInterface $form, array $options): void
277277
{
278+
$view->vars['duplicate_preferred_choices'] = $options['duplicate_preferred_choices'];
279+
278280
if ($options['expanded']) {
279281
// Radio buttons should have the same name as the parent
280282
$childName = $view->vars['full_name'];

0 commit comments

Comments
 (0)