Symfony2 - form_start function customise in twig

半腔热情 提交于 2019-12-03 10:45:58

问题


Form helpers form_start and form_end are useful in twig:

{{ form_start(form) }}
{{ form_end(form) }}

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.

Can I do it? Should i set up it into the FormType.php?

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}) }}

But in this case, for example, the class does not appear.


回答1:


As form_start has the following signature,

form_start(view, variables)

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.

Then, try ...

{{ form_start(form, {'class': 'myclass', 'action': 'myaction', 'attr': {'class': 'your_class_name'}}) }}

Also ...

  • You should be sure that your Type fileds are well defined in order to let the form_start helper set the right form-enctype.

  • The form_enctype(formView) helper is deprecated since Symfony 2.3.



来源:https://stackoverflow.com/questions/18826440/symfony2-form-start-function-customise-in-twig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!