form_widget : Add classes to existing classes

Viewed 720

I want to add classes to existing classes with twig, when invoking the form_widget()-function, so I'd like to merge them. But when I use

{{ form_widget(form.field1), {'attr': {'class': 'foo'}} }}

on an object that already has some classes attached to it by the controller, it doesn't add the class(es), but replaces them. According to the documantation this would

render a widget, but add a "foo" class to it

But the formulation isn't clear about, if the classes are really "added" or if they are replaced.

It seems that I would need to have a way to access the existing attributes/classes and merge them together with the ones I want to add. How can I accomplish this?

1 Answers
Related