从 twig 模板中的 FormView 获取全局表单错误 [英] Get global form errors from the FormView in twig template

查看:23
本文介绍了从 twig 模板中的 FormView 获取全局表单错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于在 twig 模板中呈现表单错误,您只需要使用 form_errors twig 宏,如果它是全局表单错误或字段错误,则没有区别.

但在我的情况下,全局错误不会像字段错误一样呈现,所以我不能在这两种情况下使用 form_errors 树枝宏.我决定将宏用于字段错误 &我想从 SymfonyComponentFormFormView 对象中获取全局表单错误.目标是迭代树枝模板中的全局错误 &像我想要的那样渲染它们.

实际上,我在 symfony2 文档中找不到任何可以帮助我的资源.

解决方案

终于自己找到了解决方案.对于想要做同样事情的人,解决方案是调用 $formView->get("errors") 这会给你一个 FormError 数组p>

For rendering form errors in a twig template, you just have to use the form_errors twig macro without difference if it is a global form error or a field error.

But in my case, a global error is not rendered like a field error, so I can't use the form_errors twig macro for the two cases. I decide to use the macro for the field error & I would like to get the global form errors from the SymfonyComponentFormFormView object. The goal is to iterate the global errors in the twig template & render them like I want.

Actually, I don't find any ressources on the symfony2 documentation which can help me.

解决方案

Finally, I found the solution by myself. For the people who want to do the same thing, the solution is to call $formView->get("errors") which gives you an array of FormError

这篇关于从 twig 模板中的 FormView 获取全局表单错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆