更新时未选中Yii2复选框列表


Yii2 Checkboxlist not checked on update

更新期间未选择复选框列表。这是我的密码。

if(!$model->isNewRecord)
    $list2 = ArrayHelper::map($tax_rule_group_shop->find()->where("id_tax_rules_group =".$model->id_tax_rules_group)->all(),'id_shop','id_shop'); 
 else
    $list2 = array(); 
     $tax_rule_group_shop->id_shop = $list2;
     $listdata=ArrayHelper::map(Shop::find()->all(),'id_shop','name'); ?>
    <?= $form->field($tax_rule_group_shop, 'id_shop[]')->checkboxList($listdata,$list2)->label('Shops');?

这个代码有什么问题?

如果我使用Chtml::复选框,它是有效的,但像往常一样,验证不起作用。

<?= Html::checkboxList('id_shop', $list2, $listdata, ['itemOptions'=>['class' => 'test']]); ?>
  1. 正如努里丁·拉希多夫回答的那样$model->isNewRecord应该是$tax_rule_group_shop->isNewRecord

  2. 对于$list2,请使用

    ArrayHelper::getColumn($tax_rule_group_shop->find()
    ->where("id_tax_rules_group =".$model->id_tax_rules_group)->all(),
    'id_shop')
    
  3. 我会把"哪里"改写成

    其中('id_tax_rules_group=:tid',[':tid'=>$model->id_tax_rules_group])->all()

  4. 中的$list2

    $form->field($tax_rule_group_shop, 'id_shop[]')
    ->checkboxList($listdata,$list2)->label('Shops'); 
    

    需要采用格式

    'options' => [
      'value1' => ['checked' => true],
      'value2'=> ['checked' => true]
    ],
    

    资源:checkbox列表(),activeCheckboxList()