Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 set Entity property after form submit/validation

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?

like image 458
Asciiom Avatar asked Oct 22 '22 06:10

Asciiom


1 Answers

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:

$myForm['someProperty']->setData( false );

but I expect it's better practice to fully reconstruct the form again as per your first line.

like image 119
Tim Avatar answered Oct 29 '22 13:10

Tim