本文介绍了Symfony2 - Twig:在下拉列表中设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我在我的树枝模板中有一个表单。 其中一个表单字段是从我的表单构建器中的实体创建的下拉列表。 我想在我的下拉列表中设置默认值,作为例如第28号。 我不知道如何在树枝上这样做。 我尝试了这个: {{form_widget(form.type,{value:28})}} 但没有改变,我仍然有默认的firs值。 $ b $我知道我可以在FormType类中设置一个默认值,我正在创建我的表单生成器,但是我正在寻找一个twig的方式,在我看来,它比在formType类中创建对象更优雅。 解决方案确定,发现我的错误,代码应该是: {{form_widget(form.type,{value:28})}} 我忘记了id的引号。而且,还需要CTRL + F5来刷新页面并看到差异。 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. 这篇关于Symfony2 - Twig:在下拉列表中设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-26 04:39
查看更多