Symfony2 - 在树枝中自定义 form_start 函数 [英] Symfony2 - form_start function customise in twig
问题描述
表单助手 form_start
和 form_end
在 twig 中很有用:
Form helpers form_start
and form_end
are useful in twig:
{{ form_start(form) }}
{{ form_end(form) }}
我可以自定义一些参数,例如 method
或 action
.但我需要自定义其他参数,如 class
或添加 form-enctype
.
I can customise some parameters like the method
or the action
. But I need to customise others parameters like the class
or add the form-enctype
.
我可以吗?我应该将它设置到 FormType.php
中吗?
Can I do it? Should i set up it into the FormType.php
?
从现在开始,我只是尝试将自定义值添加到 twig
函数中,如下所示:
Since now I simply try to add my customised value to the twig
function like below:
{{ form_start(form, {'class': 'myclass', 'action': 'myaction'}) }}
// fields...
{{ form_end(form, {'render_rest': true}) }}
但在这种情况下,例如 class
并没有出现.
But in this case, for example, the class
does not appear.
推荐答案
由于 form_start
有 跟随签名,
form_start(view, variables)
因为 class
不代表一个有效的变量名.您需要使用 attr
属性将您的类指定为 key/value
数组.
And as class
doesn't represent a valid variable name. You need to specify your class as a key/value
array using the attr
attribute.
那么,试试……
{{ form_start(form, {'action': 'myaction', 'attr': {'class': 'your_class_name'}}) }}
还有……
您应该确保您的 Type 字段定义良好,以便让
form_start
助手设置正确的form-enctype
.
form_enctype(formView)
帮助器自 Symfony 2.3 起已弃用.
The form_enctype(formView)
helper is deprecated since Symfony 2.3.
这篇关于Symfony2 - 在树枝中自定义 form_start 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!