I am submitting a symfony2 form, and I would like to set a certain Entity property to false if the email field for that entity was not filled and that property was submitted as 'true'.
I do this now by:
$myForm = $this->createForm(new FormType(), $myEntity);
$myForm->handleRequest($request);
if ($myForm->isValid()) {
if (!$myEntity->getEmail()) {
$myEntity->setProperty(false);
}
}
I would now expect the checkbox corresponding to the property to be uncheck when the form is displayed after being submitted. But the property checkbox in the form does not respond to that, it stays checked.
Does anyone know how to do this properly?
I think this is because your form has already been bound to the entity. The form has taken the entity's data and is not updated when the entity changes. You can operate directly on the form with:
but I expect it's better practice to fully reconstruct the form again as per your first line.