如何检查表格中的树枝是否有错误? [英] How to check if form has errors in twig?
问题描述
除了直接附加在表单字段上的特定于表单字段的错误消息外,我想在表单上方显示一条消息,指出表单包含错误。
Beside the form field specific error messages directly attached to the form field I would like to display a message above the form that the form contains errors.
我该如何检查Symfony3树枝模板是否存在错误? 在Symfony2中曾经是这样的东西:
How can I check in a Symfony3 twig template if a form has errors? There used to be something like this in Symfony2:
{% if form.get('errors') is not empty %}
<div class="error">Your form has errors. Please check fields below.</div>
{% endif %}
但这在Symfony3中不起作用。有任何想法吗? ( form.vars.errors
不起作用。)
But this doesn't work in Symfony3. Any ideas? (form.vars.errors
doesn't work.)
推荐答案
使用 form.vars.errors
:
{% if form.vars.errors is not empty %}
{# ... #}
{% endif %}
注意!请注意,如果您的根表单有错误(或者子表单有错误并且允许冒泡直到根形式)。如果表单中的常规子元素有错误,则该结果不会为空!
Attention! Note that this just evaluatues to true, if your root form has errors (or if child forms have errors and allow bubbling the error up to the root form). If regular child elements of your form have errors, this will not evaluate to empty!
因此 valid
变量为可能更合适:
So the valid
variable is probably of more suitable:
{% if not form.vars.valid %}
{# ... errors ! #}
{% endif %}
这篇关于如何检查表格中的树枝是否有错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!