Reputation: 26350
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?
Upvotes: 11
Views: 18352
Reputation: 4308
In a form type class (working in Symfony 4+, not sure if in earlier), in setDefaultOptions()
:
class FooType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('bar')
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults([
'data_class' => 'App\Entity\Foo',
'attr' => ['class' => 'custom-css-class']
]);
}
}
Upvotes: 2
Reputation: 715
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' } }) }}
Upvotes: 24