How to set a CSS ID attribute to a Symfony2 form input How to set a CSS ID attribute to a Symfony2 form input symfony symfony

How to set a CSS ID attribute to a Symfony2 form input


You can do it when you render your field in twig, if you set your id outside of the 'attributes' array like so:

{{ form_widget(form.field, { 'id': 'my_custom_id',  'attr': { 'class' : 'my_custom_class }} )}}


At twig level, you can simply do this:

{{ form_widget(form. title, { 'id': 'title-field' }) }}

Tested on Symfony 2.3.4


Since an HTML element cannot have multiple ID's, what you're trying to accomplish isn't possible because Symfony already uses ID's on form elements.

The way to solve this would be to change your JavaScript to use classes instead and using

->add('title', null, array(    'attr' => array(        'class'=>'title-field'    )))

The other way is to change the ID's your JavaScript uses to the Symfony ones.