I'm working on a symfony project which is already live and use doctrine. The problem I describe below is new and I'm certain it was caused by me but I can not confirm this as I'm unsure how to debug.
I have a simple form (it submits two fields) and I can see both these values have been submitted correctly by dumping the following:
$form->handleRequest($request);
The problem is that although I can see the submitted values, the form does not pass validation:
if ($form->isValid() === false)
This used to pass validation, perhaps 4 weeks ago so its something I've done recently.
I have dumped $form->getErrors(true);
to see if I can find the error but it returns this:
RenderController.php on line 20:
FormErrorIterator {#685 ▼
-form: Form {#691 ▶}
-errors: []
}
So unless I'm mistaken, no errors are found here as the errors is empty.
I have read that $form->isValid()
checks the data model, i.e. the underlying entity objects.
I have recently made changes to an entity. I did this by first adding a field to the database and then going into the entity class to updating it manually.
Is it possible that I have done something wrong when adding the new field? What confuses me is that are other forms which use the new field and I have not experienced any issues with.
Should I have not done this manually? How do I debug the form further?
This is likely caused by how validation works in symfony. As you correctly wrote, $form->isValid() doesn't actually validates the form itself. Instead the underlying entity is validated.
Now here comes the tricky part: Not only fields present in the form are validated, but every field of the entity. If your form only represents part of your entity then the described effect occurs. Probably one of the added fields has the NotBlank assertion (or similar) but isn't present in your form. For a new entity, this will always fail. And because the field is not present in the form, there is also no error in the form.
To solve the problem, either extend you form, remove the assertions from the new fields or take a look at validation groups