Symfony 总是将复选框保存为零
在 Symfony 1.4.11 上,我有一个布尔字段,它通过 HTML 复选框在表单上设置。当值为 0(未选中)时,复选框的 HTML 为
<input type="checkbox" value="" name="gift_type[valid]">
当我尝试保存时,它始终保存为零,我假设是因为输入没有值。
我用于复选框的代码是由 symfony 管理生成器生成的,所以我认为它可以“开箱即用”。如果我取消选中真实值,那么它将按预期工作。
schema.yml
GiftType:
columns:
valid: { type: boolean, notnull: true, default: true }
BaseGiftTypeForm.class.php
$this->setWidgets(array(
'valid' => new sfWidgetFormInputCheckbox()
));
$this->setValidators(array(
'valid' => new sfValidatorBoolean(array('required' => false))
));
On Symfony 1.4.11 I have a boolean field that is set on a form by a HTML checkbox. When the value is 0 (unchecked) then the HTML of the checkbox is
<input type="checkbox" value="" name="gift_type[valid]">
When I try and save the from it is always saved as zero, I assume because the input has no value.
The code I am using for the checkbox is generated by the symfony admin generator so I thought it would just work "out of the box". If I uncheck a true value then that works as expected.
schema.yml
GiftType:
columns:
valid: { type: boolean, notnull: true, default: true }
BaseGiftTypeForm.class.php
$this->setWidgets(array(
'valid' => new sfWidgetFormInputCheckbox()
));
$this->setValidators(array(
'valid' => new sfValidatorBoolean(array('required' => false))
));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的 HTML 复选框看起来很有趣,
你确定它是由 symfony 生成的吗? (id 丢失,即)
无论如何,您显示的所有代码都可以,无需更改架构类型! (平@richrosa)。
sfValidatorBoolean 返回一个布尔值,然后将其传递给对象,然后保存对象。您必须调试您的模型。开箱即用,Doctrine Admin Generator 中的布尔字段工作得非常好。
您可以在 GiftTypeForm 类中添加此代码来帮助调试:
如果您没有看到“valid”键布尔值,则存在验证过程错误,如果在这里,则存在模型问题(您是否覆盖了保存方法? )。
Your HTML checkbox looks funny,
are you sure it's been generated by symfony? (the id is missing i.e.)
Anyway all the code you show is OK, there is no need to change the schema type! (ping @richrosa).
sfValidatorBoolean return a boolean value, then it's passed to the object, and then the object is saved. You have to debug your model. Out of the box, a boolean field in the Doctrine Admin Generator work perfectly fine.
You can add this code in your GiftTypeForm class to help debug:
If you don't see the "valid" key boolean, there is a validation process error, if it's here, you have a model issue (have you overwrite the save method?).
我不知道 Symfony 是如何工作的。在纯 PHP/HTML 中,复选框的值与选中或未选中无关。如果选中复选框,则其值将在表单中提交;如果未选中,则不会提交。
I'm not aware of how Symfony works. In plain PHP/HTML the value of the checkbox is independent of whether it is checked or unchecked. If a checkbox is checked it's value will be submitted in the form post; if it is unchecked it won't be submitted.
尝试将数据类型更改为tinyint。
这是一个带有默认值和标签的复选框。
Try changing your datatype to tinyint.
Here is a checkbox with a default value and a label.