Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 795
korvinko Avatar asked Oct 22 '12 07:10

korvinko


1 Answers

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.
like image 95
amik Avatar answered Oct 14 '22 10:10

amik