Symfony2 - Twig : Set the default value in the dropdownlist

血红的双手。 提交于 2019-12-01 09:01:47

问题


I have a Form in my twig template. One of the form fields is a dropdownlist that is created from an Entity in my form builder.

I would like to set the default value in my dropdown list, to be the id 28 for example.

I don't know how to do this in twig.

I tryed this:

{{ form_widget(form.type, {value: 28 } ) }}

But nothing changed, I still have the firs value that is by default.

I know that I can set a default value in the FormType class where I am creating my form builder, but I am looking for a twig way, it seems to me more elegant than to create the object in the formType class.


回答1:


Ok, found my mistake, the code should be:

{{ form_widget(form.type, {value: "28" } ) }}

I forgot the quotes for the id. And also, there is a need to CTRL + F5 to refresh the page and see the difference.




回答2:


as @Elium1984 suggested

{{ form_widget(form.type, {value: twig_var ~ "" } ) }} 

or

{{ form_row(form.type, {value: twig_var ~ "" } ) }} 


来源:https://stackoverflow.com/questions/18270513/symfony2-twig-set-the-default-value-in-the-dropdownlist

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