Skip to content

Commit 5a0d245

Browse files
committed
bug symfony#60054 [Form] Use duplicate_preferred_choices to set value of ChoiceType (aleho)
This PR was merged into the 6.4 branch. Discussion ---------- [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 symfony#58561 | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#58561 | License | MIT When the preferred choices are not duplicated an option has to be selected in the group of preferred choices. Commits ------- 9f82c85 [Form] Use duplicate_preferred_choices to set value of ChoiceType
2 parents 8b43964 + 9f82c85 commit 5a0d245

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

src/Symfony/Bridge/Twig/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')

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ public function buildView(FormView $view, FormInterface $form, array $options)
281281
*/
282282
public function finishView(FormView $view, FormInterface $form, array $options)
283283
{
284+
$view->vars['duplicate_preferred_choices'] = $options['duplicate_preferred_choices'];
285+
284286
if ($options['expanded']) {
285287
// Radio buttons should have the same name as the parent
286288
$childName = $view->vars['full_name'];

0 commit comments

Comments
 (0)