I need to display all errors above the form and display a separate error for each field. How can I do this?
6 Answers
You need to be more specific but hopefully the following can help you.
Lets assume you have a variable called form
.
{{ form_errors(form) }}
Displays global errors not specific to one field
{{ form_errors(form.email) }}
Displays errors specific to field
{{ form_row(form.email) }}
Displays form_widget form_label and form_errors for field
http://symfony.com/doc/2.0/cookbook/form/form_customization.html
Edit:
So if you want your global and field errors to be displayed in he same place you can do:
{{ form_errors(form) }}
{{ form_errors(form.field1) }}
{{ form_errors(form.field2) }}
...
{% spaceless %}
{% if not form.vars.valid %}
<div class="alert alert-error">
{{ form_errors(form) }}
{% for children in form.children %}
{% if not children.vars.valid %}
{{ form_errors(children) }}
{# or with field label
<ul>
{% for error in children.vars.errors %}
<li><b>{{ children.vars.label }}</b>: {{ error.message }}</li>
{% endfor %}
</ul>
#}
{% endif %}
{% endfor %}
</div>
{% endif %}
{% endspaceless %}
work for me in sf 2.3

- 1,253
- 3
- 15
- 25
In Symfony 3.2, to get all form errors in a template, you can use a bit hacky, but simple and working solution using form.vars.errors.form.getErrors(true)
:
<ul>
{% for error in formView.vars.errors.form.getErrors(true) %}
<li>{{ error.message }}</li>
{% endfor %}
</ul>
The trick is that:
- the original form object through the errors iterator (
formView.vars.errors.form
), - the
form.getErrors(true)
gives you a recursive iterator over all form errors.

- 5,613
- 3
- 37
- 62
I'm overriding form_div_layout.html.twig in my bundle:
{% block form_errors %}
{% spaceless %}
{% set a = false %}
{% for child in form.children %}
{% if child.get("errors") %}
{% set a = 'true' %}
{% endif %}
{% endfor %}
{% if a == true %}
<div class="alert">
{% for children in form.children %}
{{ form_errors(children) }}
{% endfor %}
</div>
{% endif %}
{% if errors|length > 0 %}
<ul>
{% for error in errors %}
{{
error.messagePluralization is null
? error.messageTemplate|trans(error.messageParameters, 'validators')
: error.messageTemplate|transchoice(error.messagePluralization, error.messageParameters, 'validators')
}}
{% endfor %}
</ul>
{% endif %}
{% endspaceless %}
{% endblock form_errors %}
Now if write form_errors(form)
it display all error in form and error over each field also indicates.

- 21,321
- 22
- 95
- 134

- 700
- 3
- 10
- 23
-
child.get("errors") doesn't work for Symfony 2.5 Anyone has a solution for this ? – Jeet Nov 07 '14 at 10:01
-
Use `vars.errors` on the new versions or just dump the form field to see the attributes. – user3502626 Jan 28 '17 at 05:42
Your form aswell as your fields all have seperate error fields to start with. Could you be more specific what you are trying to do and where your problem is?

- 712
- 5
- 9
-
Me need same errors over each fields will be display on the top form (all error in one place) with call form_errors(form), is it possible? – korvinko Oct 22 '12 at 11:27
-
Daniel already posted you the form documentation. It should help you finding something for your needs. – Schwierig Oct 22 '12 at 11:32
-
It not solve my problem. Me need same error as over each fields my form in one place over all form. {{ form_errors(form) }} Displays only global errors not specific to one field. – korvinko Oct 22 '12 at 12:46
-
My answer does solve your question. Read the documentations and it tells you right there how to do it. Even my own answer should be enough. I updated my answer hopefully you understand it now. If not read the documentation until you understand it. – d0001 Oct 22 '12 at 20:47
I revised @korvinko's script, This works for Symfony 2.6.11 `
{% block form_errors %}
{% spaceless %}
<ul>
{% for children in form.children %}
{% if not children.vars.valid %}
{% for error in children.vars.errors %}
<li>{{ children.vars.label ~ ' ' ~
error.messagePluralization is null
? error.messageTemplate|trans(error.messageParameters, 'validators')
: error.messageTemplate|transchoice(error.messagePluralization, error.messageParameters, 'validators')
}}</li>
{% endfor %}
{% endif %}
{% endfor %}
</ul>
{% if errors|length > 0 %}
<ul>
{% for error in errors %}
<li>{{
error.messagePluralization is null
? error.messageTemplate|trans(error.messageParameters, 'validators')
: error.messageTemplate|transchoice(error.messagePluralization, error.messageParameters, 'validators')
}}</li>
{% endfor %}
</ul>
{% endif %}
{% endspaceless %}
{% endblock form_errors %}
`

- 1,587
- 12
- 35
-
This isn't working in Symfony 2.7.1 anymore. 'children.vars.valid' (line 5) does not exist – Thomas Landauer Sep 19 '15 at 15:13
-
you could replace `{% if not children.vars.valid %}` with `{% if children.vars.errors|length > 0 %}` – Wouter Sioen Dec 23 '15 at 13:27