10000 do not render preferred choices as selected · symfony/symfony@c5b8190 · GitHub
[go: up one dir, main page]

Skip to content

Commit c5b8190

Browse files
committed
do not render preferred choices as selected
1 parent 6832cee commit c5b8190

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
@@ -531,6 +531,31 @@ public function testSingleChoiceWithPreferred()
531531
);
532532
}
533533

534+
public function testSingleChoiceWithSelectedPreferred()
535+
{
536+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', [
537+
'choices' => ['Choice&A' => '&a', 'Choice&B' => '&b'],
538+
'preferred_choices' => ['&a'],
539+
'multiple' => false,
540+
'expanded' => false,
541+
]);
542+
543+
$this->assertWidgetMatchesXpath($form->createView(), ['separator' => '-- sep --', 'attr' => ['class' => 'my&class']],
544+
'/select
545+
[@name="name"]
546+
[@class="my&class form-control"]
547+
[not(@required)]
548+
[
549+
./option[@value="&a"][not(@selected)][.="[trans]Choice&A[/trans]"]
550+
/following-sibling::option[@disabled="disabled"][not(@selected)][.="-- sep --"]
551+
/following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
552+
/following-sibling::option[@value="&b"][.="[trans]Choice&B[/trans]"]
553+
]
554+
[count(./option)=4]
555+
'
556+
);
557+
}
558+
534559
public function testSingleChoiceWithPreferredAndNoSeparator()
535560
{
536561
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', [

0 commit comments

Comments
 (0)
0