10000 [Form] Add option `separator` to `ChoiceType` to use a custom separat… · symfony/symfony@bde5191 · GitHub
[go: up one dir, main page]

Skip to content

Commit bde5191

Browse files
mboultoureaunicolas-grekas
authored andcommitted
[Form] Add option separator to ChoiceType to use a custom separator after preferred choices
1 parent 37f04cf commit bde5191

File tree

6 files changed

+22
-6
lines changed

6 files changed

+22
-6
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@
6868
{% set render_preferred_choices = true %}
6969
{{- block('choice_widget_options') -}}
7070
{%- if choices|length > 0 and separator is not none -%}
71-
<option disabled="disabled">{{ separator }}</option>
71+
{%- if separator_html is not defined or separator_html is same as(false) -%}
72+
<option disabled="disabled">{{ separator }}</option>
73+
{% else %}
74+
{{ separator|raw }}
75+
{% endif %}
7276
{%- endif -%}
7377
{%- endif -%}
7478
{%- set options = choices -%}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,11 @@
163163
{% set render_preferred_choices = true %}
164164
{{- block('choice_widget_options') -}}
165165
{% if choices|length > 0 and separator is not none -%}
166-
<option disabled="disabled">{{ separator }}</option>
166+
{%- if separator_html is not defined or separator_html is same as(false) -%}
167+
<option disabled="disabled">{{ separator }}</option>
168< 10000 code class="diff-text syntax-highlighted-line addition">+
{% else %}
169+
{{ separator|raw }}
170+
{% endif %}
167171
{%- endif %}
168172
{%- endif -%}
169173
{% set options = choices -%}

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
7.1
55
---
66

7+
* Add option `separator` to `ChoiceType` to use a custom separator after preferred choices (use the new `separator_html` option to display the separator text as HTML)
78
* Deprecate not configuring the `default_protocol` option of the `UrlType`, it will default to `null` in 8.0
89

910
7.0

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ public function buildView(FormView $view, FormInterface $form, array $options):
234234
'expanded' => $options['expanded'],
235235
'preferred_choices' => $choiceListView->preferredChoices,
236236
'choices' => $choiceListView->choices,
237-
'separator' => '-------------------',
237+
'separator' => $options['separator'],
238+
'separator_html' => $options['separator_html'],
238239
'placeholder' => null,
239240
'placeholder_attr' => [],
240241
'choice_translation_domain' => $choiceTranslationDomain,
@@ -342,6 +343,8 @@ public function configureOptions(OptionsResolver $resolver): void
342343
'choice_attr' => null,
343344
'choice_translation_parameters' => [],
344345
'preferred_choices' => [],
346+
'separator' => '-------------------',
347+
'separator_html' => false,
345348
'duplicate_preferred_choices' => true,
346349
'group_by' => null,
347350
'empty_data' => $emptyData,
@@ -372,6 +375,8 @@ public function configureOptions(OptionsResolver $resolver): void
372375
$resolver->setAllowedTypes('choice_translation_parameters', ['null', 'array', 'callable', ChoiceTranslationParameters::class]);
373376
$resolver->setAllowedTypes('placeholder_attr', ['array']);
374377
$resolver->setAllowedTypes('preferred_choices', ['array', \Traversable::class, 'callable', 'string', PropertyPath::class, PreferredChoice::class]);
378+
$resolver->setAllowedTypes('separator', ['string']);
379+
$resolver->setAllowedTypes('separator_html', ['bool']);
375380
$resolver->setAllowedTypes('duplicate_preferred_choices', 'bool');
376381
$resolver->setAllowedTypes('group_by', ['null', 'callable', 'string', PropertyPath::class, GroupBy::class]);
377382
}

src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_1.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
"multiple",
1919
"placeholder",
2020
"placeholder_attr",
21-
"preferred_choices"
21+
"preferred_choices",
22+
"separator",
23+
"separator_html"
2224
],
2325
"overridden": {
2426
"Symfony\\Component\\Form\\Extension\\Core\\Type\\FormType": [

src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_1.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ Symfony\Component\Form\Extension\Core\Type\ChoiceType (Block prefix: "choice")
2121
placeholder getter
2222
placeholder_attr help
2323
preferred_choices help_attr
24-
help_html
25-
help_translation_parameters
24+
separator help_html
25+
separator_html help_translation_parameters
2626
inherit_data
2727
invalid_message_parameters
2828
is_empty_callback

0 commit comments

Comments
 (0)
0