在Twig中设置Symfony 2表单字段的默认值

时间:2021-04-29 20:15:32

Is it possible to set default value of input field in Twig ?

是否可以在Twig中设置输入字段的默认值?

I am rendering form row with:

我正在渲染表格行:

{{ form_widget(form.title) }}

I want to be able to set default value like:

我希望能够设置默认值,如:

{{ form_widget(form.title, {data : 'Default title'}) }}

Is it even possible ?

它甚至可能吗?

2 个解决方案

#1


68  

Magic keyword for default value is value, not data.

默认值的Magic关键字是值,而不是数据。

{{ form_widget(form.title, {'value' : 'Default title'}) }}

#2


1  

You can do it when creating the Type as well. I think it's "cleaner" than doing it in Twig.

您也可以在创建Type时执行此操作。我认为它比在Twig中做得更“干净”。

#1


68  

Magic keyword for default value is value, not data.

默认值的Magic关键字是值,而不是数据。

{{ form_widget(form.title, {'value' : 'Default title'}) }}

#2


1  

You can do it when creating the Type as well. I think it's "cleaner" than doing it in Twig.

您也可以在创建Type时执行此操作。我认为它比在Twig中做得更“干净”。