简体   繁体   中英

Symfony2 - Twig : Set the default value in the dropdownlist

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.

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.

as @Elium1984 suggested

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

or

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM