Beside the form field specific error messages directly attached to the form field I would like to display a message above the form that the form contains errors.
How can I check in a Symfony3 twig template if a form has errors? There used to be something like this in Symfony2:
{% if form.get('errors') is not empty %}
<div class="error">Your form has errors. Please check fields below.</div>
{% endif %}
But this doesn't work in Symfony3. Any ideas? (form.vars.errors
doesn't work.)
Use
form.vars.errors
:Attention! Note that this just evaluatues to true, if your root form has errors (or if child forms have errors and allow bubbling the error up to the root form). If regular child elements of your form have errors, this will not evaluate to empty!
So the
valid
variable is probably of more suitable:With symfony 3.4 it's not possible through form.vars.errors anymore except if you have
error_bubbling = true
and formcompound = false
which is unlikely.You can either use a dirty code like this :
If you are trying to build a login form with AuthenticationUtils, use a code like this in controller :
And use a simple code like this in twig template :