Symfony2 set Entity property after form submit/validation

南楼画角 提交于 2019-12-10 14:40:23

问题


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:


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.



来源:https://stackoverflow.com/questions/17543766/symfony2-set-entity-property-after-form-submit-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!