简体   繁体   中英

Yii2 Checkboxlist not checked on update

Checkboxlist is not selected during update. here is my code.

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');?

What is the problem in this code?

If i use Chtml::checkbox it is working but as usual validations are not working.

<?= Html::checkboxList('id_shop', $list2, $listdata, ['itemOptions'=>['class' => 'test']]); ?>
  1. as Nuriddin Rashidov replied !$model->isNewRecord should be !$tax_rule_group_shop->isNewRecord

  2. for $list2 use

     ArrayHelper::getColumn($tax_rule_group_shop->find() ->where("id_tax_rules_group =".$model->id_tax_rules_group)->all(), 'id_shop') 
  3. I would rewrite "where" to

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

  4. $list2 in

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

    needs to be in format

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

    Resources: checkboxList() , activeCheckboxList()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM