Get global form errors from a FormView form template

To display form errors in a branch template, you just need to use the form_errorstwig macro no matter if it is a global form error or a field error.

But in my case, the global error does not appear as a field error, so I cannot use the twig macro form_errorsfor these two cases. I decided to use a macro for a field error, and I would like to get global form errors from an object Symfony\Component\Form\FormView. The goal is to iterate over global errors in the branch template and display them the way I want.

In fact, I cannot find any resources in the symfony2 documentation that can help me.

+3
source share
3 answers

Finally, I found the solution myself. For people who want to do the same, the solution is to call $formView->get("errors"), which gives you an arrayFormError

+5
source

I am using symfony 2.5 and it worked perfectly for me that way.

Mycontroller

$error = new FormError(ErrorMessages::USER_NOT_AUTHENTICATED);
$form->addError($error);

Myview

{% for error in form.vars.errors %}
  <div class="alert alert-danger" role="alert">
    {{ error.messageTemplate|trans(error.messageParameters, 'validators')~'' }}
  </div>
{% endfor %}

hope this saves time.

+3
source

in symfony 2.3, all access methods have been removed in favor of public properties to improve performance.

$formView->get("errors");

Now:

$formView->vars["errors"];

Visit UPGRADE-2.1.md and refer to the Deviations section for more information.

+2
source

All Articles