12

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

Thomas Landauer
  • 7,857
  • 10
  • 47
  • 99
korvinko
  • 700
  • 3
  • 10
  • 23

6 Answers6

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) }}
...
fabpico
  • 2,628
  • 4
  • 26
  • 43
d0001
  • 2,162
  • 3
  • 20
  • 46
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

striker
  • 1,253
  • 3
  • 15
  • 25
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.
amik
  • 5,613
  • 3
  • 37
  • 62
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.

Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
korvinko
  • 700
  • 3
  • 10
  • 23
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?

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

`

FZE
  • 1,587
  • 12
  • 35