Akhmed
Akhmed

Reputation: 1179

How to merge Symfony form type attributes with attributes from form_row in twig

Suppose we have Symfony form type and a row adding field

$builder->add('name', 'text', ['attr' => ['class' => 'firstName', placeholder => 'first name']]);

I want this to be merged with attributes set in twig template:

{{ form_row(form.name, {'attr':{'class':'newClass'}}) }}

Currently it does replace. What is the best way to solve it?

Upvotes: 0

Views: 1548

Answers (1)

Med
Med

Reputation: 2063

You can use form variables to get form class attr, then concatene another class attr:

{{ form_row(form.name, {'attr':{'class':'newClass ' ~ form.name.vars.attr.class|default('')}}) }}

EDIT:

Corrected my previous answer after reading https://symfony.com/doc/current/reference/forms/twig_reference.html#reference-form-twig-variables

Upvotes: 1

Related Questions