Symfony2 - Twig : 在下拉列表中设置默认值 [英] 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.
我想在下拉列表中设置默认值,例如 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 : 在下拉列表中设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!