Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to acess Symfony 2 formview.valid variable in twig template

I have tried to use the variable form.valid in a twig template rendering a form, but I get an error starting with

Method "valid" for object "Symfony\Component\Form\FormView" does not exist

Here is the code generating this error

{% if form.valid == false %}
  <p class="error"> There is an error in the form. please Correct. </p>
{% endif %}

So my question is: is this variable already available or we have to explicitly set it in the controller, or am I doing something wrong?

Thanks in advance.

like image 996
Doogy1st Avatar asked Feb 01 '26 21:02

Doogy1st


1 Answers

The method isValid() is from Form class and not from FormView, which is the one that you are passing to the template when on your controller you return $form->createView().

According to the Symfony2 docs, you can check the validity of a form with:

{% if form.vars.valid == false %}
   <p class="error"> There is an error in the form. please Correct. </p>
{% endif %}
like image 130
João Alves Avatar answered Feb 04 '26 01:02

João Alves



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!