12

I need to display all errors above the form and display a separate error for each field. How can I do this?

6 Answers 6

12

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) }}
...
Sign up to request clarification or add additional context in comments.

Comments

11
{% 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

Comments

11

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:

  1. the original form object through the errors iterator (formView.vars.errors.form),
  2. the form.getErrors(true) gives you a recursive iterator over all form errors.

Comments

10

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.

2 Comments

child.get("errors") doesn't work for Symfony 2.5 Anyone has a solution for this ?
Use vars.errors on the new versions or just dump the form field to see the attributes.
0

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?

4 Comments

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?
Daniel already posted you the form documentation. It should help you finding something for your needs.
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.
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.
0

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 %}

`

2 Comments

This isn't working in Symfony 2.7.1 anymore. 'children.vars.valid' (line 5) does not exist
you could replace {% if not children.vars.valid %} with {% if children.vars.errors|length > 0 %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.