Checkboxlist Checked On Update Time

Hi,

I have a checkBoxlist in _form

<?php echo $form->labelEx($model,‘genre’); ?>

<?php echo $form->checkBoxList($model,‘genre’,CHtml::listData(Genre::model()->findAll(),‘id’,‘genre_name’)); ?>

<?php echo $form->error($model,‘genre’); ?>

i want to show checked my checboxlist while updating the form.

Thanks in Advance.

Dear Friend

Before saving it we can convert the array to string to store it in the database.

Before displaying it, we can convert the attribute value again into array.

This way when validation fails or during update the checkbox list or listBox with multiple selection is nicely updated.




public function actionCreate()

        {

                $model=new Habit;


                if(isset($_POST['Habit']))

                {

                        $model->attributes=$_POST['Habit'];

                        if($model->habits!=='')

                                $model->habits=implode(',',$model->habits);//converting to string...

                        if($model->save())

                                $this->redirect(array('view','id'=>$model->id));

                }

               $model->habits=explode(',',$model->habits);//converting to array...

                $this->render('create',array(

                        'model'=>$model,

                ));

        }


public function actionUpdate($id)

        {

                $model=$this->loadModel($id);


                if(isset($_POST['Habit']))

                {

                        $model->attributes=$_POST['Habit'];

                        if($model->habits!=='')

                                $model->habits=implode(',',$model->habits);

                        if($model->save())

                                $this->redirect(array('view','id'=>$model->id));

                }

        $model->habits=explode(',',$model->habits);

                $this->render('update',array(

                        'model'=>$model,

                ));

        }



I hope it would be helpful.

Regards.

Very nice Help . It helped me a lot.

It works! Thank you very much!

many many thanks sir!

Thanks.

Isn’t it better to implode it in beforeSave and explode it in afterFind methods?

Nice code…

wowww I was looking for this.

this is very helpful. thank you :)

How can we get the checked values in cgridview?

Using Html::encode? I cant retrieve the name of the values any other way?? plz suggest me

Thanks it worked

I am doing in yii 2.0

i have menu table(menu_id,menu_name)

and role table(role_id,role_name)

now i want to set menu permissions for each role with checkboxlist and want to update the assigned permissions latter too.

so i want to maintain a seperate table named permission with role_id and permited menu_id(s).

how i need to make the table structure and what r the changes to be done if my menu table is a dynamic one so that all my dynamic menues get populated and among them the permited menues gets preselected while the update operation is called for a particular role. thanks in advance