Symfony form how to add class to form group

In my case I needed to override the form_row block:

{% block form_row -%}
    <div class="form-group myclass{% if (not compound or force_error|default(false)) and not valid %} has-error{% endif %}">
        {{- form_label(form) -}}
        {{- form_widget(form) -}}
        {{- form_errors(form) -}}
    </div>
{%- endblock form_row %}

as suggested, the docs helps to understand why.


I actually found a way of doing it in PHP (Symfony 4.4.2). Class has to go into 'row_attr' instead of 'attr'. For the given example it'd be:

->add('name', TextType::class, [
                'label' => 'Name',
                'required' => true,
                'row_attr' => [
                    'class' => 'myclass'
                ],
            ])

related docs: https://symfony.com/doc/current/reference/forms/types/text.html#row-attr

Tags:

Forms

Symfony