“更新”页面上不会显示复选框列表框

时间:2015-05-07 08:18:15

标签: php checkbox yii2 checkboxlist

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

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

然后我使用此代码将checkboxlist复选框保存为字符串:

$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,
        ]);
    }
}

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

1 个答案:

答案 0 :(得分:1)

经过一些实验,我的猜测是你只需要更改ActiveField中的属性名称:

// input string
var inputString = "Hi {nom}, you can buy {var1}" // or $('textarea').val();

// prepare mapping object
var mappingDetails = {
    '{nom}' : 'Manuel',
    '{apl}' : 'García',
    '{var1}' : 'chips',
    '{var2}' : 'deportes y aventura',
    '{var3}' : 'informática y tecnología',
    '{cst1}' : 'Serrano, 28',
    '{cst2}' : 'Plaza del carmen, 32',
    '{cst3}' : 'García Luna, 15'
}

// get the regex
var generatedRegex = new RegExp(Object.keys(mappingDetails).join("|"),"gi");

// replace the placeholders
inputString = inputString.replace(generatedRegex , function(matchedText){
  return mappingDetails [matchedText];
});

// output
console.log(inputString);

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

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

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

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