Customize widget's ID attribute in Twig

前端 未结 3 374
南旧
南旧 2021-01-04 00:50

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

Passing {\'attr

相关标签:
3条回答
  • 2021-01-04 00:58

    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
    
    0 讨论(0)
  • 2021-01-04 00:59

    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.

    0 讨论(0)
  • 2021-01-04 01:03
    {{ form_widget(form.title, { 'attr': {'id': 'my_id'} }) }}
    

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

    I have used it and it works.

    0 讨论(0)
提交回复
热议问题