简体   繁体   中英

Symfony. form_widget auto select for dropdown

How can I auto select an option in a form_widget ?

{{ form_widget(form.group, {'attr':{'value':varGroup }}) }}

The previous snippet only works for text fields, not dropdown. form.group is a list of groups, and varGroup is the group I need to be selected by default.

Is it possible doing it by using form_widget capabilities?

Thanks a lot in advance!

I ran into this issue a few weeks ago, for some reason varGroup isn't evaluated within the form_widget function call and strange things (read: I can't remember exactly) are set as the value of the <select> .

And this doesn't work:

{{ form_widget(form.group, {'attr': {'value': {{ varGroup }} } }) }} // Plus it's hideous.

The workaround I found was to cast the variable to a string by using the concat operator ~ with an empty string '' .

{{ form_widget(form.group, {'attr': {'value': varGroup ~ '' } }) }}

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