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.
seenivasan
(Chellamnivas)
March 22, 2013, 11:27am
2
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.
priyranjan
(Singh Priyranjan)
April 15, 2013, 5:57am
3
Very nice Help . It helped me a lot.
Season
(Wu Xuesong)
May 8, 2013, 8:47am
4
It works! Thank you very much!
shgninc
(Shgninc)
December 2, 2013, 5:49am
7
Isn’t it better to implode it in beforeSave and explode it in afterFind methods?
wowww I was looking for this.
this is very helpful. thank you
don.vijay05
(don.vijay05@gmail.com)
August 11, 2014, 12:19pm
10
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
OverDose
(Yiigiijg)
April 12, 2015, 6:59pm
12
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