Symfony2 set Entity property after form submit/val

2019-06-21 06:18发布

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?

1条回答
一夜七次
2楼-- · 2019-06-21 06:54

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.

查看更多
登录 后发表回答