Skip to content

Commit

Permalink
feature #52447 [Form] Add option separator to ChoiceType to use a…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
nicolas-grekas committed Jan 9, 2024
2 parents 9afd335 + bde5191 commit 4328f5b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 6 deletions.
Expand Up @@ -68,7 +68,11 @@
{% set render_preferred_choices = true %}
{{- block('choice_widget_options') -}}
{%- if choices|length > 0 and separator is not none -%}
<option disabled="disabled">{{ separator }}</option>
{%- if separator_html is not defined or separator_html is same as(false) -%}
<option disabled="disabled">{{ separator }}</option>
{% else %}
{{ separator|raw }}
{% endif %}
{%- endif -%}
{%- endif -%}
{%- set options = choices -%}
Expand Down
Expand Up @@ -163,7 +163,11 @@
{% set render_preferred_choices = true %}
{{- block('choice_widget_options') -}}
{% if choices|length > 0 and separator is not none -%}
<option disabled="disabled">{{ separator }}</option>
{%- if separator_html is not defined or separator_html is same as(false) -%}
<option disabled="disabled">{{ separator }}</option>
{% else %}
{{ separator|raw }}
{% endif %}
{%- endif %}
{%- endif -%}
{% set options = choices -%}
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
7.1
---

* 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)
* Deprecate not configuring the `default_protocol` option of the `UrlType`, it will default to `null` in 8.0
* Add a `keep_as_list` option to `CollectionType`
* Add a new `model_type` option to `MoneyType`, to be able to cast the transformed value to `integer`
Expand Down
Expand Up @@ -236,7 +236,8 @@ public function buildView(FormView $view, FormInterface $form, array $options):
'expanded' => $options['expanded'],
'preferred_choices' => $choiceListView->preferredChoices,
'choices' => $choiceListView->choices,
'separator' => '-------------------',
'separator' => $options['separator'],
'separator_html' => $options['separator_html'],
'placeholder' => null,
'placeholder_attr' => [],
'choice_translation_domain' => $choiceTranslationDomain,
Expand Down Expand Up @@ -344,6 +345,8 @@ public function configureOptions(OptionsResolver $resolver): void
'choice_attr' => null,
'choice_translation_parameters' => [],
'preferred_choices' => [],
'separator' => '-------------------',
'separator_html' => false,
'duplicate_preferred_choices' => true,
'group_by' => null,
'empty_data' => $emptyData,
Expand Down Expand Up @@ -374,6 +377,8 @@ public function configureOptions(OptionsResolver $resolver): void
$resolver->setAllowedTypes('choice_translation_parameters', ['null', 'array', 'callable', ChoiceTranslationParameters::class]);
$resolver->setAllowedTypes('placeholder_attr', ['array']);
$resolver->setAllowedTypes('preferred_choices', ['array', \Traversable::class, 'callable', 'string', PropertyPath::class, PreferredChoice::class]);
$resolver->setAllowedTypes('separator', ['string']);
$resolver->setAllowedTypes('separator_html', ['bool']);
$resolver->setAllowedTypes('duplicate_preferred_choices', 'bool');
$resolver->setAllowedTypes('group_by', ['null', 'callable', 'string', PropertyPath::class, GroupBy::class]);
}
Expand Down
Expand Up @@ -18,7 +18,9 @@
"multiple",
"placeholder",
"placeholder_attr",
"preferred_choices"
"preferred_choices",
"separator",
"separator_html"
],
"overridden": {
"Symfony\\Component\\Form\\Extension\\Core\\Type\\FormType": [
Expand Down
Expand Up @@ -21,8 +21,8 @@ Symfony\Component\Form\Extension\Core\Type\ChoiceType (Block prefix: "choice")
placeholder getter
placeholder_attr help
preferred_choices help_attr
help_html
help_translation_parameters
separator help_html
separator_html help_translation_parameters
inherit_data
invalid_message_parameters
is_empty_callback
Expand Down

0 comments on commit 4328f5b

Please sign in to comment.