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 input
field with the attribute class
set to calendar
You can add it in the options of your form class:
You can do this from the twig template:
From http://symfony.com/doc/current/book/forms.html#rendering-each-field-by-hand
You can to this in Twig or the FormClass as shown in the examples above. But you might want to decide in the controller which class your form should get. Just keep in mind to not have much logic in the controller in general!
You can do it with FormBuilder. Add this to the array in your FormBuilder:
Renders the HTML widget of a given field. If you apply this to an entire form or collection of fields, each underlying form row will be rendered.
{# render a field row, but display a label with text "foo" #} {{ form_row(form.name, {'label': 'foo'}) }}
The second argument to form_row() is an array of variables. The templates provided in Symfony only allow to override the label as shown in the example above.
See "More about Form Variables" to learn about the variables argument.