在保存到 symfony 之前更新/验证嵌入表单

发布于 2024-10-10 02:39:02 字数 219 浏览 0 评论 0原文

我想在将嵌入的表单字段保存到数据库之前对其进行验证。目前,如果表单字段为空,它会将空值保存到数据库中。我允许空字段,但如果表单字段为空,我不想插入任何内容。

还有一个简单的问题,如何在验证/保存嵌入表单字段之前更改字段值?

$this->form->getObject 在操作中起作用,但 $this->embeddedForm->getObject 表示找不到对象

I would like to validate an embedded form field before it gets saved in the database. Currently it will save an empty value into the database if the form field is empty. I'm allowing empty fields, but I want nothing inserted if the form field is empty.

Also quick question, how to alter field values before validating/saving an embedded form field?

$this->form->getObject works in the action, but $this->embeddedForm->getObject says object not found

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

×眷恋的温暖 2024-10-17 02:39:02

我找到了一个非常简单的解决方案。解决方案是手动重写 Form 的模型类 save() 方法。

class SomeUserClass extends myUser {

public function save(Doctrine_Connection $conn = null)
    {
        $this->setFirstName(trim($this->getFirstName()));
        if($this->getFirstName())
        {
                return parent::save();
        }else
        {
                return null;
        }
    }

}

在此示例中,我检查名字字段是否为空。如果不是,则保存表格。如果它是空的,那么我们不会在表单上调用保存。

我无法让验证器正常工作,这是一个同样干净的解决方案。

I found a really easy solution. The solution is to override your Form's model class save() method manually.

class SomeUserClass extends myUser {

public function save(Doctrine_Connection $conn = null)
    {
        $this->setFirstName(trim($this->getFirstName()));
        if($this->getFirstName())
        {
                return parent::save();
        }else
        {
                return null;
        }
    }

}

In this example, I'm checking if the firstname field is blank. If its not, then save the form. If its empty, then we don't call save on the form.

I haven't been able to get the validators to work properly, and this is an equally clean solution.

心的憧憬 2024-10-17 02:39:02

Symfony 为您提供了一系列表单/对象保存过程的钩子。
您可以通过覆盖表单的 doSave() 或 processValues() 函数,使用 null 前/后验证来覆盖空白值。

您可以在这里阅读更多相关信息: http ://www.symfony-project.org/more-with-symfony/1_4/en/06-Advanced-Forms#chapter_06_ saving_object_forms

Symfony gives you a bunch of hooks into the form/object saving process.
You can overwrite the blank values with null pre/post validation using by overriding the the doSave() or processValues() functions of the form.

You can read more about it here: http://www.symfony-project.org/more-with-symfony/1_4/en/06-Advanced-Forms#chapter_06_saving_object_forms

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文