I've got a problem with a Symfony2 generated CRUD form. (With MongoDB Documents, but I do not think that this is related)
In my controller's createAction() method, when I debug the form result :
$form->isValid() // returns false $form->getErrors() // returns en empty array(0) {}
So, I do not get anything using form_errors(form)
on my twig template (which seems normal because of $form->getErrors()
empty return)
And the written values are not replaced in the form...
Does anyone has an idea?
The first thing to understand is validation is done on the model, not the form. The form can contain errors, but only if it has a field mapped to the property that doesn't validate. So if your form does not contain the invalid field (maybe a NotNull
assertion on a property that is not in the form), it will not show the error.
The second thing is that $form->getErrors()
will only show errors for that level, each form child can contain its own errors. So if you want to check the errors, you should loop through the fields and call getErrors on each field. The getErrors
method on the Form class can be deceiving that way.
To debug a form, use $form->getErrorsAsString()
instead of $form->getErrors()
.
$form->getErrorsAsString()
should only be used to debug the form...it will contain the errors of each child elements which is not the case of $form->getErrors()
.
As Peter mentions, $form->getErrors()
will not return the sum of all the errors of the children forms.
To understand how a form can be invalid and have a getErrors() returning an empty array, you can have a look at the isValid() method of the symfony form class. As you can see, there are 2 cases where the form is not valid, the first one test for the general form, and the second case test for each child elements.
public function isValid() { //... //CASE I : IF CHILD ELEMENTS HAVE ERRORS, $this->errors WILL CONTAIN //THE ERROR ON THE CHILD ELEMENT AND NOT ON THE GENERAL 'errors' FIELD //ITSELF if (count($this->errors) > 0) { return false; } //CASE II: AND THIS IS WHY WE ARE TESTING THE CHILD ELEMENTS AS WELL //TO CHECK WHETHER THERE ARE VALID OR NOT if (!$this->isDisabled()) { foreach ($this->children as $child) { if (!$child->isValid()) { return false; } } } return true; }
Therefore each form child can contain an error, but $form->getErrors()
itself won't return all the errors. Considering a form that has many child elements, you will generally have $form->getErrors() with a CSRF error if the CSRF is not correct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With