TroodoN-Mike
TroodoN-Mike

Reputation: 16165

Symfony2 createFormBuilder

This is probably easy question but I could not find answer on the official symfony2 documentation page.

This is my form:

    $form = $this->createFormBuilder($task)
         ->add('myInputField', 'text'))
         ->add('myAnotherInputFiled', 'date', array(
             'widget' => 'single_text',
             'required' => false))
         ->getForm();

Question is how to add attribute to the myInputField like "class" or "title"?

I tried "...->add('myInputField', 'text', array('class' => 'CustomClass')..." but I get "the option "class" does not exist.

I need to do it before its outputed in the view.

For any help big thanks!

Upvotes: 1

Views: 5454

Answers (1)

Louis-Philippe Huberdeau
Louis-Philippe Huberdeau

Reputation: 5431

You can add additional attributes using the attr option. I noticed it adds the attributes on both the label and the widget, but at least it gets the information through.

$form = $this->createFormBuilder($task)
     ->add('myInputField', 'text'))
     ->add('myAnotherInputFiled', 'date', array(
         'widget' => 'single_text',
         'required' => false,
         'attr' => array(
             'class' => 'CustomClass',
         ),
     ))
     ->getForm();

Upvotes: 5

Related Questions