8000 bug #35200 [TwigBridge] do not render preferred choices as selected (… · symfony/symfony@b9cc0c8 · GitHub
[go: up one dir, main page]

Skip to content

Commit b9cc0c8

Browse files
committed
bug #35200 [TwigBridge] do not render preferred choices as selected (xabbuh)
This PR was merged into the 4.4 branch. Discussion ---------- [TwigBridge] do not render preferred choices as selected | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #35135 (comment) | License | MIT | Doc PR | Commits ------- c5b8190 do not render preferred choices as selected
2 parents 290ce00 + c5b8190 commit b9cc0c8

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@
6565
{%- endif -%}
6666
{%- if preferred_choices|length > 0 -%}
6767
{% set options = preferred_choices %}
68+
{% set render_preferred_choices = true %}
6869
{{- block('choice_widget_options') -}}
6970
{%- if choices|length > 0 and separator is not none -%}
7071
<option disabled="disabled">{{ separator }}</option>
7172
{%- endif -%}
7273
{%- endif -%}
7374
{%- set options = choices -%}
75+
{%- set render_preferred_choices = false -%}
7476
{{- block('choice_widget_options') -}}
7577
</select>
7678
{%- endblock choice_widget_collapsed -%}
@@ -83,7 +85,7 @@
8385
{{- block('choice_widget_options') -}}
8486
</optgroup>
8587
{%- else -%}
86-
<option value="{{ choice.value }}"{% if choice.attr %}{% with { attr: choice.attr } %}{{ block('attributes') }}{% endwith %}{% endif %}{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice_translation_domain is same as(false) ? choice.label : choice.label|trans({}, 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) and choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice_translation_domain is same as(false) ? choice.label : choice.label|trans({}, choice_translation_domain) }}</option>
8789
{%- endif -%}
8890
{% endfor %}
8991
{%- endblock choice_widget_options -%}

src/Symfony/Bridge/Twig/Resources/views/Form/foundation_5_layout.html.twig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,14 @@
160160
{%- endif %}
161161
{%- if preferred_choices|length > 0 -%}
162162
{% set options = preferred_choices %}
163+
{% set render_preferred_choices = true %}
163164
{{- block('choice_widget_options') -}}
164165
{% if choices|length > 0 and separator is not none -%}
165166
<option disabled="disabled">{{ separator }}</option>
166167
{%- endif %}
167168
{%- endif -%}
168169
{% set options = choices -%}
170+
{%- set render_preferred_choices = false -%}
169171
{{- block('choice_widget_options') -}}
170172
</select>
171173
{%- endblock choice_widget_collapsed %}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,31 @@ public function testSingleChoiceWithPreferred()
546546
);
547547
}
548548

549+
public function testSingleChoiceWithSelectedPreferred()
550+
{
551+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', [
552+
'choices' => ['Choice&A' => '&a', 'Choice&B' => '&b'],
553+
'preferred_choices' => ['&a'],
554+
'multiple' => false,
555+
'expanded' => false,
556+
]);
557+
558+
$this->assertWidgetMatchesXpath($form->createView(), ['separator' => '-- sep --', 'attr' => ['class' => 'my&class']],
559+
'/select
560+
[@name="name"]
561+
[@class="my&class form-control"]
562+
[not(@required)]
563+
[
564+
./option[@value="&a"][not(@selected)][.="[trans]Choice&A[/trans]"]
565+
/following-sibling::option[@disabled="disabled"][not(@selected)][.="-- sep --"]
566+
/following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
567+
/following-sibling::option[@value="&b"][.="[trans]Choice&B[/trans]"]
568+
]
569+
[count(./option)=4]
570+
'
571+
);
572+
}
573+
549574
public function testSingleChoiceWithPreferredAndNoSeparator()
550575
{
551576
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', [

0 commit comments

Comments
 (0)
0