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

我只是一个虾纸丫 提交于 2019-12-03 05:31:32
mb3rnard

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 }} )}}
Żabojad

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.

Olav

I am convinced now there is no easy answer. The closest seems to in fact change the markup by using form theming. Thank you Michi for pointing this way.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!