Symfony 总是将复选框保存为零

发布于 2024-12-25 09:47:21 字数 654 浏览 1 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(3

已下线请稍等 2025-01-01 09:47:21

你的 HTML 复选框看起来很有趣,
你确定它是由 symfony 生成的吗? (id 丢失,即)

<input type="checkbox" name="gift_type[valid]" checked="checked" id="gift_type_valid" />

无论如何,您显示的所有代码都可以,无需更改架构类型! (平@richrosa)。
sfValidatorBoolean 返回一个布尔值,然后将其传递给对象,然后保存对象。您必须调试您的模型。开箱即用,Doctrine Admin Generator 中的布尔字段工作得非常好。

您可以在 GiftTypeForm 类中添加此代码来帮助调试:

  protected function doUpdateObject($values)
  {
    var_dump($values);die();
    $this->getObject()->fromArray($values);
  }

如果您没有看到“valid”键布尔值,则存在验证过程错误,如果在这里,则存在模型问题(您是否覆盖了保存方法? )。

Your HTML checkbox looks funny,
are you sure it's been generated by symfony? (the id is missing i.e.)

<input type="checkbox" name="gift_type[valid]" checked="checked" id="gift_type_valid" />

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:

  protected function doUpdateObject($values)
  {
    var_dump($values);die();
    $this->getObject()->fromArray($values);
  }

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?).

心在旅行 2025-01-01 09:47:21

我不知道 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.

巨坚强 2025-01-01 09:47:21

尝试将数据类型更改为tinyint。

valid: { phpName: Valid, type: TINYINT, size: '4', required: false }

这是一个带有默认值和标签的复选框。

$this->setWidgets(array(
'valid' => new sfWidgetFormInputCheckbox(array('label'=>'My Label') , array('value' => '1'));

Try changing your datatype to tinyint.

valid: { phpName: Valid, type: TINYINT, size: '4', required: false }

Here is a checkbox with a default value and a label.

$this->setWidgets(array(
'valid' => new sfWidgetFormInputCheckbox(array('label'=>'My Label') , array('value' => '1'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文