Reputation: 1910
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:
isValid()
returns falsegetErrors( true )
returns an error for each field{{ form_errors( form ) }}
generates no text for the form or any of the fields. 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
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
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