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.
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;
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')) ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With