复选框未选中 [英] Checkboxes are not checked

查看:54
本文介绍了复选框未选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法获取要选择的游戏的 DB 值:

I cannot get DB values of games to be selected:

Game:
  actAs:
    Timestampable: ~
  columns:
    id: { type: integer(4), primary: true, autoincrement: true, unsigned: true }
    game_name: { type: string(100), notnull: true }
    logo: { type: string(100), notnull: true, comment: "Game Logo" }
  indexes:
    it:
      fields: game_name
      type: unique

Campaign:
  actAs:
    Timestampable: ~
  columns:
    id: { type: integer(4), primary: true, autoincrement: true, unsigned: true }
    name: { type: string(100), notnull: true }
    percentage: { type: integer(4), notnull: true, unsigned: true }
    is_active: { type: integer(1), notnull: true, unsigned: true }
    start: { type: datetime, notnull: true }
    end: { type: datetime, notnull: true }

CampaignGames:
  actAs:
    Timestampable: ~
  columns:
    id: { type: integer(4), primary: true, autoincrement: true, unsigned: true }
    campaign_id: { type: integer(4), notnull: true, unsigned: true }
    game_id: { type: integer(4), notnull: true, unsigned: true }
  indexes:
    tc:
      fields: [campaign_id, game_id]
      type: unique
  relations:
    Campaign: { onDelete: CASCADE, local: campaign_id, foreign: id, foreignAlias: CampaignCampaignGames }
    Game: { onDelete: CASCADE, local: game_id, foreign: id, foreignAlias: GameCampaignGames }

我在此处添加了属于 Game 模型的游戏复选框,让用户将游戏添加到 CampaignGames,但不幸的是他们从未检查过...这些值存在于 DB 中.

I have added games checkbox here which belongs to Game model to let the user add games to CampaignGames, but unfortunately they never checked... And these values are present in DB.

class AdminconsoleCampaignForm extends CampaignForm
{

public function configure()
{
    parent::configure();

    $this->widgetSchema['is_active'] = new sfWidgetFormSelectRadio(array(
        'choices' => array(1 => 'On', 0 => 'Off'),
    ));

    $games = Doctrine_Core::getTable('Game')->getGames();

    $this->widgetSchema['game_id'] = new sfWidgetFormSelectCheckbox(array(
        'choices' => $games
    ));

    $this->validatorSchema['game_id'] = new sfValidatorChoice(array(
        'choices' => array_keys($games)
        , 'multiple' => true
        , 'required' => false
    ));

    $this->removeFields();

}

也试过用

$this->widgetSchema['game_id']->setDefault(array($data));

没有运气.如何解决?我真的坚持这一点.

No luck. How to resolve it? I'm really stuck on that.

推荐答案

有两件事引起了我的注意:

There are two things that caught my attention:

尝试将您的 schema.yml 更改为以下内容:

Try changing your schema.yml to the following:

Campaign:
  [...]
  columns:
    [...]
    is_active:
      type: boolean
      notnull: true
      default: 0 # Or whichever default value you prefer
    [...]

这样 Symfony/Doctrine 将处理与您的 Campaign 记录的 is_active 行有关的任何事情.

This way Symfony/Doctrine will take care of anything regarding the is_active row of your Campaign record.

如果您现在重建模型,您的 BaseCampaignForm.class.php 将像这样自动定义 is_active 小部件:

If you now rebuild your model your BaseCampaignForm.class.php will define the is_active widget automatically like this:

$this->setWidgets(array(
  [...]
  'is_active' => new sfWidgetFormInputCheckbox(),
  [...]
);

$this->setValidators(array(
  [...]
  'ist_active' => new sfValidatorBoolean(array('required' => false)),
  [...]
);

注意: required 设置为 false 是因为如果没有选中复选框,它也不会发布.sfValidatorBoolean 负责处理这一点并自行禁用该值.如果您将其设置为 true,则用户将无法取消选中该框并在没有验证器异常的情况下提交表单.

Note: That required is set to false is there because if a checkbox isn't selected it's not posted either. The sfValidatorBoolean takes care of this and disables the value all by itself. If you would set it to true than the user wouldn't be able to uncheck the box and submit the form without a validator exception.

在您使用的代码中:

$this->widgetSchema['game_id']->setDefault(array($data));

这行不通,因为您使用的是带有附加对象的表单(BaseFormDoctrine).所有默认值都直接从分配给该表单的对象中取出(在您的情况下为 Campaign 对象,因为您正在扩展 CampaignForm).

This won't work because you're using a Form with an object attached to it (a BaseFormDoctrine). All the default values are taken right out of the object assigned to that form (in your case a Campaign object because you're extending CampaignForm).

(主要陷阱)如果您想在表单对象上设置默认值,您必须在表单对象本身上设置它们:

$this->getObject()->setGameId($id);

无法使用对象表单的小部件设置默认对象值.这些默认值总是会被​​实际的对象值覆盖(这是有道理的,因为表单代表了对象).

Default object values can't be set using the object form's widgets. These default values will always be overwritten by the actual object values (which makes sense because the form represents the object).

很高兴能以某种方式帮助你.

Glad if I was able to help you in some way.

这篇关于复选框未选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆