Symfony 表单:保存包含复选框和/或选择小部件的表单?
我有一个包含以下字段的 Doctrine 表:
Article:
columns:
title:
type: string(40)
content:
type: string(255)
publish:
type: boolean
我正在创建一个自定义表单,用于在我的网站上创建文章。在我的自定义表单中,我有一个 publish
字段的复选框。提交表单时,如果选中该复选框,则其表单值将返回为“on”。如果未选中该复选框,则表单值不存在。
因为我正在使用扩展 Doctrine 模型表单的表单,所以我希望能够在它通过有效性时简单地执行 $this->form->save();
。
但是,如果 publish
的复选框仅给出 on
的值或没有给出任何值,则这两个值对于数据库都无效。表单保存正确,但无论是否选中该复选框,publish
的数据库值始终只是默认值。
解决这个问题的正确方法是什么?
另外,你如何处理选择 sfWidgets?传递的值是一个值数组。 $this->form-save()
似乎忽略了这些数组。
您是否必须手动从表单中获取值并从这些值创建 Doctrine Collection 并以这种方式保存?或者 Symfony 是否有更好的方法希望您使用?
I have a Doctrine table that contains the following fields:
Article:
columns:
title:
type: string(40)
content:
type: string(255)
publish:
type: boolean
I am creating a custom form for creating Articles on my site. In my custom form I have a checkbox for the publish
field. When submitting the form, if the checkbox is checked, the form value for it comes back as "on". If the checkbox is NOT checked, the form value is non-existent.
Because I'm working with a form that extends Doctrine's Model Forms, I'd like to be able to simply do a $this->form->save();
when it passes validity.
But if the checkbox for publish
only gives the value of on
or nothing, then neither value is valid for the database. The form saves properly but the database value for publish
is always just default no matter if the checkbox is checked or not.
What is the correct approach to this problem?
Also, how do you deal with choice sfWidgets? The passed values is an array of values. $this->form-save()
seems to ignore those arrays.
Do you manually have to get the values from the form and create a Doctrine Collection from the values and save it that way? Or is there a better method that Symfony expects you to use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的验证器是否用于发布 sfValidatorBoolean 实例?如果是,它应该返回 false。
您关于选择小部件的问题需要更多阐述。请粘贴表单的
configure
方法、您从$form->getValues()
接收的值以及您期望接收的值。Is your validator for publish an instance of
sfValidatorBoolean
? If it is, it should return false.Your question about choice widgets requires more elaboration. Please paste your form's
configure
method, the values you are receiving from$form->getValues()
, and what you expect to be receiving.