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?

+8
forms symfony error-handling
source share
6 answers

I override form_div_layout.html.twig in my package:

{% 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 the entry form_errors(form) displays the entire error in the form and the error above each field, also indicates.

+7
source share

You need to be more specific, but hopefully the following may help you.

Suppose you have a variable called form .

{{ form_errors(form) }} Displays global non-field errors

{{ form_errors(form.email) }} Displays field specific errors

{{ form_row(form.email) }} Displays form_widget form_label and form_errors for a field

http://symfony.com/doc/2.0/cookbook/form/form_form_customization.html

Edit:

So, if you want your global and field errors to appear in the same place, you can do:

 {{ form_errors(form) }} {{ form_errors(form.field1) }} {{ form_errors(form.field2) }} ... 
+10
source share
 {% 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

+6
source share

In Symfony 3.2, to get all the form errors in the template, you can use a slightly 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:

  • the original form object through the error iterator ( formView.vars.errors.form ),
  • form.getErrors(true) gives you a recursive iterator over all form errors.
+2
source share

Your form, as well as your fields, have separate error fields. Could you clarify what you are trying to do and where is your problem?

0
source share

I reworked the @korvinko 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 %} 

`

0
source share

All Articles