8000 feature #52447 [Form] Add option `separator` to `ChoiceType` to use a… · symfony/symfony@4328f5b · GitHub
[go: up one dir, main page]

Skip to content

Commit 4328f5b

Browse files
feature #52447 [Form] Add option separator to ChoiceType to use a custom separator after preferred choices (mboultoureau)
This PR was squashed before being merged into the 7.1 branch. Discussion ---------- [Form] Add option `separator` to `ChoiceType` to use a custom separator after preferred choices | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Linked with #52260 | License | MIT Add `separator_html` option to display the `separator` text as HTML Commits ------- bde5191 [Form] Add option `separator` to `ChoiceType` to use a custom separator after preferred choices
2 parents 9afd335 + bde5191 commit 4328f5b

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+
{% 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
* Add a `keep_as_list` option to `CollectionType`
910
* Add a new `model_type` option to `MoneyType`, to be able to cast the transformed value to `integer`

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ public function buildView(FormView $view, FormInterface $form, array $options):
236236
'expanded' => $options['expanded'],
237237
'preferred_choices' => $choiceListView->preferredChoices,
238238
'choices' => $choiceListView->choices,
239-
'separator' => '-------------------',
239+
'separator' => $options['separator'],
240+
'separator_html' => $options['separator_html'],
240241
'placeholder' => null,
241242
'placeholder_attr' => [],
242243
'choice_translation_domain' => $choiceTranslationDomain,
@@ -344,6 +345,8 @@ public function configureOptions(OptionsResolver $resolver): void
344345
'choice_attr' => null,
345346
'choice_translation_parameters' => [],
346347
'preferred_choices' => [],
348+
'separator' => '-------------------',
349+
'separator_html' => false,
347350
'duplicate_preferred_choices' => true,
348351
'group_by' => null,
349352
'empty_data' => $emptyData,
@@ -374,6 +377,8 @@ public function configureOptions(OptionsResolver $resolver): void
374377
$resolver->setAllowedTypes('choice_translation_parameters', ['null', 'array', 'callable', ChoiceTranslationParameters::class]);
375378
$resolver->setAllowedTypes('placeholder_attr', ['array']);
376379
$resolver->setAllowedTypes('preferred_choices', ['array', \Traversable::class, 'callable', 'string', PropertyPath::class, PreferredChoice::class]);
380+
$resolver->setAllowedTypes('separator', ['string']);
381+
$resolver->setAllowedTypes('separator_html', ['bool']);
377382
$resolver->setAllowedTypes('duplicate_preferred_choices', 'bool');
378383
$resolver->setAllowedTypes('group_by', ['null', 'callable', 'string', PropertyPath::class, GroupBy::class]);
379384
}

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