在保存到 symfony 之前更新/验证嵌入表单
我想在将嵌入的表单字段保存到数据库之前对其进行验证。目前,如果表单字段为空,它会将空值保存到数据库中。我允许空字段,但如果表单字段为空,我不想插入任何内容。
还有一个简单的问题,如何在验证/保存嵌入表单字段之前更改字段值?
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了一个非常简单的解决方案。解决方案是手动重写 Form 的模型类 save() 方法。
在此示例中,我检查名字字段是否为空。如果不是,则保存表格。如果它是空的,那么我们不会在表单上调用保存。
我无法让验证器正常工作,这是一个同样干净的解决方案。
I found a really easy solution. The solution is to override your Form's model class save() method manually.
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.
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