korvinko
korvinko

Reputation: 700

Display all errors with form_errors(form) plus for each field in symfony2

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

Upvotes: 12

Views: 42049

Answers (6)

korvinko
korvinko

Reputation: 700

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.

Upvotes: 10

amik
amik

Reputation: 5909

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.

Upvotes: 11

d0001
d0001

Reputation: 2190

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) }}
...

Upvotes: 12

FZE
FZE

Reputation: 1627

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

`

Upvotes: 0

striker
striker

Reputation: 1263

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

Upvotes: 11

Schwierig
Schwierig

Reputation: 712

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?

Upvotes: 0

Related Questions