Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a class attribute to a Symfony2 form input

How can I set the HTML class attribute to a form <input> using the FormBuilder in Symfony2 ?

Something like this:

->add('birthdate', 'date',array(
      'input' => 'datetime',
      'widget' => 'single_text',
      'attr' => array(
          'class' => 'calendar'
      )
 ))

 {{ form_widget(form.birthdate) }}

I want this inputfield with the attribute class set to calendar

like image 979
Jean-Philippe Bond Avatar asked Jul 18 '11 14:07

Jean-Philippe Bond


2 Answers

You can do this from the twig template:

{{ form_widget(form.birthdate, { 'attr': {'class': 'calendar'} }) }}

From http://symfony.com/doc/current/book/forms.html#rendering-each-field-by-hand

like image 175
Derek Stobbe Avatar answered Oct 09 '22 18:10

Derek Stobbe


You can do it with FormBuilder. Add this to the array in your FormBuilder:

'attr'=> array('class'=>'span2')
like image 115
Acyra Avatar answered Oct 09 '22 18:10

Acyra