问题描述
我的树枝模板中有一个表格.其中一个表单字段是从我的表单构建器中的实体创建的下拉列表.
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.
我想在我的下拉列表中设置默认值,例如 id 28.
I would like to set the default value in my dropdown list, to be the id 28 for example.
我不知道如何在 twig 中执行此操作.
I don't know how to do this in twig.
我试过这个:
{{ form_widget(form.type, {value: 28 } ) }}
但没有任何改变,我仍然有默认的第一个值.
But nothing changed, I still have the firs value that is by default.
我知道我可以在创建表单构建器的 FormType 类中设置默认值,但我正在寻找一种树枝方式,在我看来这比在 formType 类中创建对象更优雅.
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" } ) }}
我忘记了 id 的引号.还有,需要CTRL+F5来刷新页面看看有什么区别.
I forgot the quotes for the id. And also, there is a need to CTRL + F5 to refresh the page and see the difference.
这篇关于Symfony2 - Twig:在下拉列表中设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!