[英]Symfony. form_widget auto select for dropdown
How can I auto select an option in a form_widget
?如何在
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. form.group
是组列表, varGroup
是我默认需要选择的组。
Is it possible doing it by using form_widget
capabilities?是否可以通过使用
form_widget
功能来实现?
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>
.几周前我遇到了这个问题,由于某种原因
varGroup
没有在form_widget
函数调用中进行评估,奇怪的事情(阅读:我记不清了)被设置为<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 ''
.我发现的解决方法是使用concat 运算符
~
和空字符串''
将变量转换为字符串。
{{ form_widget(form.group, {'attr': {'value': varGroup ~ '' } }) }}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.