简体   繁体   中英

get value from checkboxlist yii2

Here I got 2 views, first view is a form which let user to register. Then save information to DB in controller and refer to the other view.

I write a checkbox list in first view.

  <?= $form->field($model, 'items[]')->checkboxList(['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C']); ?>

Then I tried to get the value from it in controller and save to DB.

if ($model->load(Yii::$app->request->post()) && $model->validate()) {    
//save to DB
    $model = new EntryForm();
    $tableMember=new Members;
    $tableMember->select=$model-> items ;
    $tableMember->save();
    return $this->render('entry-confirm', ['model' => $model]);  
}

to show in entry-confirm.php

<li><label>Selected</label>: <?php echo Html::encode($model->items['a']) ?></li>

but it's empty.

I used NetBeans debugger, it shows:

$_POST = [
    '_csrf' => 'OTFHYUpIaVJNSxAJPBEDGV8DcTYjAhojAFofVx0HJmULVCwoAiRENA==',
    'EntryForm' => [
        'username' => 'df',
        'email' => '2@c.c',
        'password' => '123',
        'items' => [
            'a',
            'b',
        ],
        'country' => '',
    ],
];

It seems that items did get attributes. Is there the other way to create checkboxes? Or how can I get the values from a checkbox list?

try this way :

<?php
echo $form->checkBoxList($model,'items',
    array('a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C'),                      
      );
?>

this checkbox list should be part of Form Widget, and Items should be your databse field or Variable in your class.First try to check your model fields are coming on your view or not.

Try this one

 $tableMember->select = implode(",", $model-> items);

$model->items returns array of checked checkbox.

well, I always have issues getting back arrays with something like

$model->items

(as in values from eg checkboxlist), I find it easier to get the values with eg $_POST['EntryForm']['items'] , like this:

$model->items=implode(',',$_POST['EntryForm']['items']);

(done in the controller, before $model->save() )

as an example:

(we split the post and save action)

if ($model->load(Yii::$app->request->post())) {

      $model->items=implode(',',$_POST['EntryForm']['items']); //change items into string to be saved

    if($model->save()){

         return $this->redirect(['view', 'id' => $model->id]);

            }

  } else {

      $model->items=explode(',',$model->items); //string to array to fill the checkboxlist

      return $this->render('create', [
          'model' => $model, 
      ]);
  }

the main issue for $model->items not to work is that probably it is not considered "safe", meaning that it has not been declared in the models under rules (public function rules(), eg adding

[['items'], 'string', 'max' => 250],

or

[['items'], 'safe'],

should do the trick....

see also: Yii2 - Models - Safe Attributes

HTH

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