How can i add attribute to optgroup in symfony 4 form ?
choice_attr ? attr ? ...
Thanks for answers.
How can i add attribute to optgroup in symfony 4 form ?
choice_attr ? attr ? ...
Thanks for answers.
Unfortunately it doesn't seems possible. If you dig in the code of twig : https://github.com/symfony/twig-bridge/blob/4.4/Resources/views/Form/form_div_layout.html.twig
This is how the optgroup is rendered :
<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>
As you can see, nothing is planned to put anything else than a label on it.
The only way I see is to create your own custom ChoiceType, that would probably simply have the original one as parent, with an option "optgroup_attr" or you'd put it in "choices" under a specific format.
Then you copy paste the original view of it in a custom form theme ( https://symfony.com/doc/current/form/create_custom_field_type.html ), and you're free to access the create options and render it as you want (just get inspiration from the original theme).