How to check if there is an error in the branch?

Besides the error messages of the form field that are directly related to the form field, I would like to display a message above the form in which the form contains errors.

How to check Symfony3 template template if form has errors? There was 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 does not work in Symfony3. Any ideas? ( form.vars.errorsdoes not work.)

+4
source share
2 answers

Use form.vars.errors:

{% if form.vars.errors is not empty %}
    {# ... #}
{% endif %}

! , true, ( ). , !

valid, , :

{% if not form.vars.valid %}
    {# ... errors ! #}
{% endif %}
+13

symfony 3.4 form.vars.errors, , error_bubbling = true form compound = false, .

:

{% set errors = false %}
{% for child in form.children %}
        {% if child.vars.errors is defined and child.vars.errors|length %}
                {% set errors = true %}
        {% endif %}
{% endfor %}
{% if errors %}
        [...]
{% endif %}

AuthenticationUtils, :

//Get the login error if there is one
if ($error = $authenticationUtils->getLastAuthenticationError()) {
        //Add error message to mail field
        $form->get('mail')->addError(new FormError($error->getMessageKey()));
}

//Last username entered by the user
if ($lastUsername = $authenticationUtils->getLastUsername()) {
        $form->get('mail')->setData($lastUsername);
}

//Render view
return $this->render('@ExampleBundle/template.html.twig', array('form' => $form->createView(), 'error' => $error));

, :

{% if error %}
        [...]
{% endif %}
-1

Source: https://habr.com/ru/post/1625657/


All Articles