2

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

1
  • isn't that already done ?! you have an error message or what ? Commented May 25, 2014 at 20:29

2 Answers 2

4

If I understand your question, you want to set attributes but on your main form. Then, you should rely on $builder->setAttribute('data-custom', 'foo') but, be aware this solution will set the attribute on the div wrapping your form not the form tag itself.

If you want to add attribute on the form tag, you will need to do it in your template via {{ form_start(form, { 'attr': { 'data-custom': 'foo' }}) }}

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, this is exactly what I wanted to know :)
in my case, with SF 2.8, the builder has an effect on the form tag, on SF 3.3, the effect is on the div has you said
0

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'],
        ]);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.