Symfony2 invalid form without errors Symfony2 invalid form without errors symfony symfony

Symfony2 invalid form without errors


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.


Update for Symfony 2.6

So depending on your Symfony2 version:

die($form->getErrorsAsString());

As of , the getErrorsAsString() function is deprecated (will be removed in Symfony3) and you should use the following method:

die((string) $form->getErrors());     // Main errorsdie((string) $form->getErrors(true)); // Main and child errors

As of , you can also use the dump (dev environment) function if you have activated the DebugBundle:

dump((string) $form->getErrors());     // Main errorsdump((string) $form->getErrors(true)); // Main and child errors