Symfony: sfWidgetFormInputCheckbox() “开启” vs 教义形式正确?
我制作了一个自定义表单,它扩展了 Doctrine 对象的 Doctrine 基本表单。
我有一个类型为:布尔值的数据库字段。对于我的表单,我用 sfWidgetFormInputCheckbox()
小部件表示。当我的表单提交时,如果选中了复选框,则其值将返回为 on
;如果未选中,则不会传递任何值。不管怎样,当我执行 $form->save()
时,布尔字段不受影响。 Symfony/Doctrine 希望看到 true
或 false
。不是 on
或根本没有值。
我的解决方案只是查看该值是否已传递,如果是,则该字段为 true
else false
:
$values['my_field'] = isset($values['my_field']) ? true : false;
这是处理此问题的正确方法吗?我认为 Symfony/Doctrine 已经了解如何处理复选框。我希望它知道如果复选框的值返回为 on
然后在数据库中将该值设置为 true
如果它不存在,那么这是假
。
显然我的方法有效......但是正确的方法是什么?
I have a custom Form I've made that extends the Doctrine base forms for a Doctrine object.
I have a database field that is type: boolean. For my form, I have this represented by a sfWidgetFormInputCheckbox()
widget. When my form is submitted, the value of my checkbox comes back as on
if it was checked and if it wasn't checked, then no value is passed. Either way, when I do my $form->save()
, the boolean field is not affected. Symfony/Doctrine wants to see a true
or false
. Not a on
or no value at all.
My solution is just to see if the value was passed at all and if so, then that field is true
else false
:
$values['my_field'] = isset($values['my_field']) ? true : false;
Is this the proper way to handle this? I would think that Symfony/Doctrine would already understand how to handle checkboxes. I would expect it to know that if the value for the checkbox comes back as on
then to set that value in the database as true
and if it doesn't exist, then it's false
.
Obviously my approach works... but what is the CORRECT approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
sfValidatorBoolean
返回 true 或 false。如果绑定表单后没有得到 true 或 false,则说明您没有使用此验证器。sfValidatorBoolean
returns true or false. If you are not getting true or false after binding the form, you are not using this validator.