Symfony2 - Add class to form tag

2020-04-10 01:09发布

问题:

I know I can add classes to <input> fields in a form like this:

->add('foo', 'text', array('attr' => array('class' => 'foo-class'));

but how could I add a class to a <form> tag?

回答1:

For this, there's 2 solutions, either you do it in your controller or your view.

1) In your controller :

$form = $this->createForm(new FormType(), $data, ['attr' => ['class' => 'myClass']]);

2) In your view (Twig) :

{{ form_start(form, { 'attr' : { 'class': 'myClass' } }) }}


标签: forms symfony