Symfony2 - Twig : Set the default value in the dro

2020-03-28 17:19发布

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.

2条回答
小情绪 Triste *
2楼-- · 2020-03-28 17:39

as @Elium1984 suggested

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

or

{{ form_row(form.type, {value: twig_var ~ "" } ) }} 
查看更多
在下西门庆
3楼-- · 2020-03-28 17:53

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.

查看更多
登录 后发表回答