Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checking checkboxList in Yii2 at the time of updating post?

Tags:

php

mysql

yii2

I want to checked Yii2 CheckboxList at time of post update my list of options is mention below enter image description here

<?= $form->field($category,'title')->checkboxList([1=>'Latest news','2'=>'Unit Performance','3'=>'Latest Technology'])->label(FALSE); ?>

I want to check some item at time of update which is selected at the time of post creation like latest news .

enter image description here

Kindly help me

like image 666
Ram Choubey Avatar asked Dec 30 '15 12:12

Ram Choubey


People also ask

Is it possible to add checklist field using Yii?

We can add checklist field in simple way using yii. Sorry, something went wrong. Sign up for free to join this conversation on GitHub . Already have an account?

Does checkboxlist check by default when form loads?

@black-room-boy As for checking something by default (when form loads), it depends on how you render your checkBoxList? Whether you do this by a simple HTML helper classes or as an ActiveField, being part of ActiveForm.

How to assign $checkedlist array to $category->title?

If option "Latest news" and "Unit Performance" is selected so, on update selected option value array will be $checkedList = [1, 2]; So, Simply assign $checkedList array to $category->title . Like as, Show activity on this post.


1 Answers

Use line of code of example.

 $list = [1=>'Latest news','2'=>'Unit Performance','3'=>'Latest Technology'];

<?= $form->field($category,'title')->checkboxList($list)->label(FALSE); ?>

If option "Latest news" and "Unit Performance" is selected so, on update selected option value array will be $checkedList = [1, 2];

So, Simply assign $checkedList array to $category->title. Like as,

$category->title = $checkedList;

Full example is:

 $list = [1=>'Latest news','2'=>'Unit Performance','3'=>'Latest Technology'];

 if(!$category->isNewRecord) {
     $checkedList = [1, 2]; //get selected value from db if value exist
     $category->title = $checkedList;
 }

<?= $form->field($category,'title')->checkboxList($list)->label(FALSE); ?>
like image 50
GAMITG Avatar answered Nov 09 '22 06:11

GAMITG