复选框列表框不会在更新页面上显示为已选中

Checkboxlist boxes won't appear checked on Update page

我在创建和更新视图上都有一个复选框列表:

<?php echo $form->field($model3, 'options[]')->checkboxList(['CPF' => 'CPF', 'FWL' => 'FWL', 'SDL' => 'SDL', 'CDAC' => 'CDAC']); ?>

然后我使用这段代码将复选框列表复选框保存为字符串:

$model3->options = implode(',',  $model3->options);

更新后,我使用此代码将其转换回数组:

$model3->options = explode(",", $model3->options);

我希望根据创建时选中的内容,我的更新页面中应该有选中的框。但是复选框列表框仍未选中。

这是我的控制器:

public function actionCreateNewStaff()
{ 
  $session = Yii::$app->session;
  if($session['user_type']=='SuperAdmin'){
      $model1 = new User();
      $model2 = new UserModule();
      $model3 = new Employee();


      if($model1->load(Yii::$app->request->post()) && $model2->load(Yii::$app->request->post()) && $model3->load(Yii::$app->request->post())){
        $model1->user_type = 'BizStaff';
        $model1->status = 1;
        date_default_timezone_set('Asia/Manila');
        $model1->date_added = date('Y/m/d', time());
        $model1->avatar = UploadedFile::getInstance($model1, 'avatar');
        $model1->creator_id = Yii::$app->user->identity->_id;
        $model1->parent = $session['store_owner'];
        $model1->password = sha1($model1->password);


          if($model1->save()){
            $model2->user_id = $model1->_id; 
            $model2->save();

            $model3->user_id = $model1->_id; 
           if(isset($model3->options)){
             $model3->options = implode(',',  $model3->options);
           } 
            $model3->save();

            Yii::$app->session->setFlash('success', "Success!");
          }
          else{
            Yii::$app->session->setFlash('error', "User Registration Failed!");
          }


        return $this->redirect(['managestaff']);

      } else {              
            return $this->renderAjax('createstaff', [
              'model1' => $model1,
              'model2' => $model2,
              'model3' => $model3,
            ]);
      }
    }else{
      return $this->goHome();
  }

}

public function actionUpdateEmployee($id)
{  

    $session = Yii::$app->session;
    $model1 = $this->findModel($id);
    $password = $model1->password;
    $model2 = UserModule::find()->where( [ 'user_id' => new \MongoId($id) ] )->one(); 
    $model3 = Employee::find()->where( [ 'user_id' => new \MongoId($id) ] )->one();
    if(isset($model3->options)){
      $model3->options = explode(",", $model3->options);
    }  
    $username = $model1->username;

    if($model1->load(Yii::$app->request->post()) && $model2->load(Yii::$app->request->post()) && $model3->load(Yii::$app->request->post())) {

      if($model1->password != $password)
        $model1->password = sha1($model1->password);
      else
        $model1->password = $password;

      date_default_timezone_set('Asia/Manila');
      $model1->date_added = date('Y/m/d', time());


        $model1->save();
        if($model1->save()){
          $model2->user_id= $model1->_id;
          $count = count($session['modules']);
          foreach($session['modules'] as $module){
            if(is_int($model2->{$module})){
                $model2->{$module} = null; //unchecked
            }
            else{  
                $model2->{$module}= '1'; //checked
            }
          } 
          $model2->save(); 
          if(isset($model3->options)){
            $model3->options = implode(',',  $model3->options);
          }  
          $model3->save();
          Yii::$app->session->setFlash('success', "User successfully updated!");
          return $this->redirect(['viewemployee', 'id' => (string)$model1->_id]);

      }  
    } else {
        return $this->render('updateemployee', [
            'model1' => $model1,
            'model2' => $model2,
            'model3' => $model3,
        ]);
    }
}

如何在我的更新页面上显示这些复选框?

经过一些试验,我猜测您只需更改 ActiveField 中的属性名称:

//from
<?php echo $form->field($model3, 'options[]')->checkboxList([....]); ?>

//to
<?php echo $form->field($model3, 'options')->checkboxList([....]); ?>

我希望这是正确的。你还应该做的是内爆:

$model3->options = empty($model3->options) ? '' : implode(',', $model3->options);

因为可能没有选中任何复选框。然后一个空字符串将被发送到服务器。