Search code examples
symfonysymfony-2.7

How to show symfony validation errors?


I try to visualize my error messages from the validator. When i write the following example:-

{{ form_errors(form.pGWeek) }}

it works fine and i get the message. But my form has 200 fields and so it's not practical.

So i want to iterate over an Array with all messages, at the end of the form like this:

{% if form.name.vars.errors|length > 0 %}
  <ul class="form-errors name">
   {% for error in form.name.vars.errors %}
        {{ error }}
 {% endfor %}
  </ul>
{% endif %}

But i did not get some messages. As well i tried some another versions.. but nothing worked. I'm using Symfony 2.7.

Can give me somebody a tip?

Thanks for a short feedback.


Solution

  • So you just want to show all errors for all children of a form without displaying them beside each input field?
    Then you could iterate over all the children of your form, check if any errors appeared on this children and if so, iterate over all errors of this children. That could be something like that:

       {% for children in form.children %}
           {% if children.vars.errors is defined %}
               {% for error in children.vars.errors %}
                   {#{{ dump(children) }}#}
                   {#{{ dump(error) }}#}
                   {{ dump(children.vars.name ~ ': ' ~ error.message) }}
                {% endfor %}
            {% endif %}
       {% endfor %}
    

    Which result in an error like description: This value should not be blank..