从 twig 模板中的 FormView 获取全局表单错误表单、全局、模板、错误

2023-09-07 09:45:34 作者:饭团

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

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

如何在Mac上从PDF表单中提取数据

实际上,我在 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