Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

yii2

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.

like image 539
Cristus Cleetus Avatar asked Apr 15 '14 15:04

Cristus Cleetus


2 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;
like image 138
Cristus Cleetus Avatar answered Oct 07 '22 00:10

Cristus Cleetus


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')) ?>
like image 30
Андрей Бережков Avatar answered Oct 07 '22 00:10

Андрей Бережков