Skip to content

Commit b6ba798

Browse files
committed
Merge branch '6.4' into 7.2
* 6.4: [Form] Use duplicate_preferred_choices to set value of ChoiceType
2 parents be77539 + acf7783 commit b6ba798

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
@@ -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 -%}

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')

0 commit comments

Comments
 (0)