Symfony2 invalid form without errors

Flo Schild picture Flo Schild · Jun 26, 2012 · Viewed 68.5k times · Source

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?

Answer

Peter Kruithof picture Peter Kruithof · Jun 27, 2012

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.