Home » Php » php – How to have Yii2.0 CheckboxList items as checked as the form loads?

php – How to have Yii2.0 CheckboxList items as checked as the form loads?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am trying to use a Activeform checkboxlist widget in YII 2.0 Framework

in my case, i have an array called “list” which has the names of languages which will be rendered out as checkboxlist, now i am able to do that, but i want the check boxes to be checked as the form loads.

$list = [0 => 'PHP', 1 => 'MySQL', 2 => 'Javascript'];
$list2 = [0,2];

using the following line, i am able to acheive what i want using HTML helper classes :

<?= Html::checkboxList('CuisineId',$list2,$list); ?>

but i want to be able to do this using the Activeform Widget CheckboxList, which as per documentation is to be used in the following way :

static checkboxList( $items, $options = [] )

So in my case, i have figured how to pass the parameter for $items, which is in the following way:

<?= $form->field($record, 'CuisineId')->checkboxlist($list);?> 

But now i do not know how to pass the parameters that will allow the checkboxes to be checked.

How to&Answers:

Okay i got the solution from yiiframework forum itself.

The solution was just to add the following line of code and it worked! :

$record->CuisineId = $list2;

Answer:

Solve
database

view

Controller

$model = $this->findModel($id);
    $model->INDEXES =ArrayHelper::getColumn($model->publicationIndexes, 'INDEXES_ID');

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

View (_form)

<?= $form->field($model, 'INDEXES')->checkboxList(ArrayHelper::map(Indexes::find()->all(), 'ID', 'NAME')) ?>