'Symfony 2: setting form attributes in Form Class

I have this form class, in my Symfony 2 project. Everything works fine, but I don't know where to set up form tag attributes.

namespace Forms\FormsBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class RegistrationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('username', 'email', array(
                'label' => 'Enter Username',
                'attr'  => array(
                    'class' => 'form-control',
                    'placeholder'   => 'Username',
                    'data-trigger'  => 'change',
                    'data-required' => 'true',
                    'data-type'     => 'email',
                )
            ))
            ->add('password', 'password', array(
                'label' => 'Enter Password',
                'attr'  => array(
                    'class' => 'form-control',
                    'placeholder' => 'Password',
                    'data-trigger'  => 'change',
                    'data-required' => 'true',
                    'data-type'     => 'password'
                    )
            ))
            ->add('send', 'submit', array(
                'attr'  => array(
                    'class' => 'btn btn-primary',
                    'placeholder' => 'Send'
                    )
            ));
    }

public function getName()
    {
        return 'registration';
    }

}

Does anyone know if, it's possible to set attributes of form here? E.g. class="something" data-something="true"

Thank you very much for any advice



Solution 1:[1]

You can add html attributes to the <form> element in the configureOptions method of your formType:

class FoobarType extends AbstractType
{
    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'attr' => ['data-test' => 'hello'],
        ]);
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Simon Epskamp