Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9f82c85

Browse filesBrowse 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 8b43964 commit 9f82c85
Copy full SHA for 9f82c85

File tree

3 files changed

+53
-1
lines changed
Filter options

3 files changed

+53
-1
lines changed

‎src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
+1-1
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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Tests/Extension/AbstractDivLayoutTestCase.php
+50
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
+2
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.