I want to checked Yii2 CheckboxList at time of post update my list of options is mention below
<?= $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
.
Kindly help me
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?
@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.
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.
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); ?>
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