如何显示 Twig 中的所有错误?
How can I show all errors in Twig?
我在 Symfony 5 中创建了一个表单,当验证错误时,我希望如果错误为真,将所有错误显示在一个中,如下所示:
错误:
- 错误 1
- 错误 2
- 错误 3
...
我使用:
{% if form_errors(form)|length %}
<div>Errors:</div>
{{ form_errors(form) }}
{% endif %}
但不起作用。
我如何在 Twig 中执行此操作?
您使用以下显示所有错误
{#
If the form is not valid then :
Note: in this case the form variable is : form
#}
{% if not form.vars.valid %}
<ul>
{# Loop through every form item #}
{% for child in form.children %}
{# Display the errors of the form item #}
{%for error in child.vars.errors%}
<li>{{error.message}}</li>
{%endfor%}
{%endfor%}
</ul>
{%endif%}
您可以在代码中使用它:
{% if not form.vars.valid %}
<div>Errors:</div>
<ul>
{% for error in form.vars.errors.form.getErrors(true) %}
<li>{{ error.message }}</li>
{% endfor %}
</ul>
{% endif %}
我在 Symfony 5 中创建了一个表单,当验证错误时,我希望如果错误为真,将所有错误显示在一个中,如下所示: 错误:
- 错误 1
- 错误 2
- 错误 3 ...
我使用:
{% if form_errors(form)|length %}
<div>Errors:</div>
{{ form_errors(form) }}
{% endif %}
但不起作用。
我如何在 Twig 中执行此操作?
您使用以下显示所有错误
{#
If the form is not valid then :
Note: in this case the form variable is : form
#}
{% if not form.vars.valid %}
<ul>
{# Loop through every form item #}
{% for child in form.children %}
{# Display the errors of the form item #}
{%for error in child.vars.errors%}
<li>{{error.message}}</li>
{%endfor%}
{%endfor%}
</ul>
{%endif%}
您可以在代码中使用它:
{% if not form.vars.valid %}
<div>Errors:</div>
<ul>
{% for error in form.vars.errors.form.getErrors(true) %}
<li>{{ error.message }}</li>
{% endfor %}
</ul>
{% endif %}