Search code examples
phpformssymfonyinputformbuilder

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


Solution

  • 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