Symfony2 在表单提交/验证后设置实体属性 [英] Symfony2 set Entity property after form submit/validation

查看:29
本文介绍了Symfony2 在表单提交/验证后设置实体属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在提交一个 symfony2 表单,如果该实体的电子邮件字段未填写且该属性提交为true",我想将某个实体属性设置为 false.

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'.

我现在这样做:

$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:

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

但我希望按照您的第一行再次完全重建表单是更好的做法.

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

这篇关于Symfony2 在表单提交/验证后设置实体属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆