David Patterson
David Patterson

Reputation: 1910

Symfony 2.6.3 {{ form_errors( form ) }} is blank when isValid() returns false

I'm using Symfony V2.6.3

I have a simple form with three fields based on a Type class.

The Entity class that the Type class specifies via setDefaultOptions() has the use Symfony\Component\Validator\Constraints as Assert; statement.

Each field has an @Assert\NotBlank() constraint in the Entity class.

CSRF is enabled.

HTML5 validation is disabled.

If I submit the form with all fields blank, the following happens:

I created a custom form theme and modified {% block form_errors %} to dump the errors variable. The dump shows the errors property is a FormErrorIterator object with two properties: form, which is, I believe the field definition, and errors which is an empty array.

Oddly enough, drilling down into the form property reveals an errors property that is an array with a single FormError object that contains the error message.

This is not my first time using forms. It has worked just fine for me in the past. Could this be a new bug in 2.6?

I have searched for this and all I found were situations where getErrors() was also returning nothing.

Thanks in advance, Dave

Upvotes: 3

Views: 1254

Answers (2)

David Patterson
David Patterson

Reputation: 1910

Okay. I finally found the answer to this question.

Many thanks to thenetimp on the Symfony forum. He had the same problem and figured it out (http://forum.symfony-project.org/viewtopic.php?f=23&t=42841&p=135003&hilit=form_errors#p135003).

Turns out that you have to call createView() after calling isValid(). That actually makes sense when you think about it.

Upvotes: 3

Wouter J
Wouter J

Reputation: 41934

Form errors are bound to a FormType, not to the complete Form. The RootType will only contain the errors that bubbled up from sub types. The sub types will only contain the errors of their type and the ones bubbled up from sub sub types, etc.

Doing {{ form_errors(form) }} will only render the errors of the root form. Assume there was an error on a form.name field, you can get this error by doing: {{ form_errors(form.name) }}.

Upvotes: 2

Related Questions