Customize widget's ID attribute in Twig

倾然丶 夕夏残阳落幕 提交于 2019-11-30 16:36:42

问题


Is it possible to customize HTML 'id' attribute in Twig when rendering a widget with {{ form_widget(form.NAME_OF_THE_FIELD) }} ?

Passing {'attr': {'id': 'SOMETHING'}} doesn't work...

Thanks for help!


回答1:


Did you try: {{ form_widget(form.title, { 'id': 'my_custom_id' }) }}?

If this still doesn't work, you could also rely on form customization to handle it. But this could be overkill and may not be the best idea. Check the documentation on form theming for more info.

As I said, maybe there is better way but this is only what I can propose you for now.

Hope this helps.




回答2:


{{ form_widget(form.title, { 'attr': {'id': 'my_id'} }) }}

http://symfony.com/doc/current/book/forms.html

I have used it and it works.




回答3:


I don't know if it can help for googlers but you also can specify the prefix of the attribut in the formType

public function getName()
{
   return 'vendor_testbundle_sickness';
}

your attribute will have the folowing id :

vendor_testbundle_sickness_SOMETHING


来源:https://stackoverflow.com/questions/8213014/customize-widgets-id-attribute-in-twig

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