-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Group choices when expanded is true #19514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@mysterty just to make things more clear. Do you think this is a bug we should fix or better explain in the docs ... or are you asking to add this feature to the Form component? Thanks! |
According to the doc, it's a bug, cause it isn't said it works only when |
If @mysterty means "Allow expanded choices to be grouped, too" then I'm +1. Feature request. |
Can be solved by having something like this in the templates: {%- block choice_widget_expanded -%}
<div {{ block('widget_container_attributes') }}>
{% for name, choices in form.vars.choices %}
{% if choices is iterable %}
<label class="choice_category">
<strong>
{{ choice_translation_domain is same as(false) ? name : name|trans({}, choice_translation_domain) }}
</strong>
</label>
<div>
{% for key,choice in choices %}
{{ form_widget(form[key]) }}
{{ form_label(form[key]) }}
{% endfor %}
</div>
{% else %}
{{- form_widget(form[name]) -}}
{{- form_label(form[name], null, {translation_domain: choice_translation_domain}) -}}
{% endif %}
{% endfor %}
{%- endblock choice_widget_expanded -%} |
Hello, |
Hi,
|
Hi, With your answers, I did this version that works fine for me : {%- block choice_widget_expanded -%}
<div {{ block('widget_container_attributes') }}>
{% if form.vars.choices|length != form.children|length %}
{% for name, choices in form.vars.choices %}
<div class="checkbox-group">
{% for key,choice in choices %}
{{- form_widget(form[key]) -}}
{{- form_label(form[key]) -}}
{% endfor %}
</div>
{% endfor %}
{% else %}
{%- for child in form %}
{{- form_widget(child) -}}
{{- form_label(child, null, {translation_domain: choice_translation_domain}) -}}
{% endfor -%}
{% endif %}
</div>
{%- endblock choice_widget_expanded -%}
Then, you just have to define your In my case, I used this .checkbox-group:not(:last-child) {
border-bottom: 1px solid #ccc;
padding-bottom: 10px;
} |
#5489 (comment) works for me on Symfony 4.4. However, the semantically correct way to do this, is to use |
Anyone wants to work on a PR for this? |
I can have a try, which solution do you think we have to implement? |
I guess this has to be implemented into all form themes ( I could commit it for Which branch am I supposed to commit to? |
Thank you for this suggestion. |
@carsonbot Yes :-) |
Someone who is interested in this feature will need to create a pull request against the |
Thank you for this suggestion. |
Could I get an answer? If I do not hear anything I will assume this issue is resolved or abandoned. Please get back to me <3 |
Hey, I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen! |
Since this capability still hasn't been added to Symfony 5, I've enhanced tbordinat's answer to include the placeholder if anyone needs it included in the choice list.
|
Six years later, SF 6 and still not solved 😁 |
Hi @kevinpapst For SF6 {% block choice_widget_expanded -%}
{%- endblock choice_widget_expanded %} |
For me, It's the first time I ever needed it, and I've been working with Symfony since 2012. I guess "it's just not needed" is half of the reason, the other half is "it's not as simple as just throwing {%- block choice_widget_options -%}
{% for group_label, choice in options %}
{%- if choice is iterable -%}
<optgroup label="{{ choice_translation_domain is same as(false) ? group_label : group_label|trans({}, choice_translation_domain) }}">
{% set options = choice %}
{{- block('choice_widget_options') -}}
</optgroup>
{%- else -%}
<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.labelTranslationParameters, choice_translation_domain) }}</option>
{%- endif -%}
{% endfor %}
{%- endblock choice_widget_options -%} Another complication is that the code above is tapping into the Long story short, in my "solution," I walk the array of form groups and their choices, and then I find the {%- block choice_widget_expanded -%}
{%- set attr = attr|merge({ class: attr.class|default('mt-2') }) -%}
<div {{ block('widget_container_attributes') }}>
{%- set options = choices -%}
{{- block('choice_widget_expanded_options') -}}
</div>
{%- endblock choice_widget_expanded -%}
{%- block choice_widget_expanded_options -%}
{%- for group_label, option in options -%}
{%- if option is iterable -%}
<fieldset>
<legend>{{ choice_translation_domain is same as(false) ? group_label : group_label|trans({}, choice_translation_domain) }}</legend>
{%- set options = option -%}
{{- block('choice_widget_expanded_options') -}}
</fieldset>
{%- else -%}
<div>
{%- set child = form.children|filter(child => child.vars.value == option.value)|first -%}
{{- form_widget(child) -}}
{{- form_label(child, null, {
translation_domain: choice_translation_domain,
label_attr: label_attr|merge({class: 'label-standard -ml-2 pl-2 pr-2'}),
required: false,
}) -}}
</div>
{%- endif -%}
{% endfor %}
{% endblock %} I'm curious to know other people's alternatives. |
I personally used @janklan's solution, I don't see anything wrong with it, it could probably be copy-pasted in the default form theme of the next symfony release. |
That's a pretty brave statement not even I would make :) |
@Alexandre-Fernandez hold your horses with "copy&paste in the default form theme", (1) I've already found the first thing I broke, and (2) I suspect there is more elegant way to use the form rendering mechanism with all the magical block names and prefixes. The thing that broke was a missing placeholder choice. The reason it broke is because the choice never appears in the array of grouped options, and the template is no longer rendering all Here is an updated version: {%- block choice_widget_expanded_options -%}
{#- See https://github.com/symfony/symfony/issues/19514#issuecomment-2022062889 -#}
{%- if form.children.placeholder is defined -%}
{%- with {child: form.children.placeholder} -%}
{{- block('choice_widget_expanded_option') -}}
{%- endwith -%}
{%- endif -%}
{%- for group_label, option in options -%}
{%- if option is iterable -%}
<fieldset>
<legend>{{ choice_translation_domain is same as(false) ? group_label : group_label|trans({}, choice_translation_domain) }}</legend>
{%- set options = option -%}
{{- block('choice_widget_expanded_options') -}}
</fieldset>
{%- else -%}
{%- with {child: form.children|filter(child => child.vars.value == option.value)|first} -%}
{{- block('choice_widget_expanded_option') -}}
{%- endwith -%}
{%- endif -%}
{%- endfor -%}
{%- endblock -%}
{%- block choice_widget_expanded_option -%}
<div>
{%- set label_attr = child.vars.label_attr -%}
{{- form_widget(child) -}}
{{- form_label(child, null, {
translation_domain: choice_translation_domain,
label_attr: label_attr|merge({class: 'label-standard -ml-2 pl-2 pr-2'}),
required: false,
}) -}}
</div>
{%- endblock -%} |
This is why I said "probably", my point still remains however, we could just add something similar to what you have done with minor changes and call it a day. This issue is 8 years old, and the documentation implies that this should work (no mention that it doesn't when |
Uh oh!
There was an error while loading. Please reload this page.
Hello,
Actually choices are grouped when using the
group_by
capability only if expanded isfalse
.It should be also possible to group choices also when expanded is
false
:http://symfony.com/doc/current/reference/forms/types/entity.html#group-by
The text was updated successfully, but these errors were encountered: