Skip to content Skip to sidebar Skip to footer

Need Listbox With Multiple Selection - In Yii

I need listbox of multiple selection in yii, i have code of form area but its saving to database as a word 'Array' in field, How to handle this problem? how to get back while view

Solution 1:

For me works this:

'multiple'=>true

Your code must be something like that:

<?php echo $form->dropDownList($model,'clients', 
  CHtml::listData(client::model()->findAll(array('order'=>'id')), 'id', 'name'),
     array('empty'=>'','multiple'=>true ,'style'=>'width:400px;','size'=>'10'));
?>

Solution 2:

$htmlOptions = array('size' => '5', 'multiple' => 'true','style'=>'width: 333px');

$model->field_id    =   array_of_data_to_be_selected

$form->listBox($model,'field_id',$listData, $htmlOptions);


Solution 3:

keep this code in controller

$arr = implode(",",$model->attributes['hobbies']);

$model->hobbies=$arr;

in controler create,update in the first if condition

in database you can see the values with comma as delimiter


Solution 4:

How does this work in CHtml::listBox()

if(!empty($htmlOptions['multiple']))
{
    if(substr($name,-2)!=='[]')
        $name.='[]';
}

So you can try this

<?php echo $form->dropDownList($model,'clients', 
    CHtml::listData(client::model()->findAll(array('order'=>'id')), 'id', 'name'),
    array(
>>>     'name'=>CHtml::resolveName($model, 'clients').'[]',
        'empty'=>'',
        'multiple'=>'multiple',
        'style'=>'width:400px;',
        'size'=>'10',
    )
);?>

But it's better to use CHtml::listBox()


Post a Comment for "Need Listbox With Multiple Selection - In Yii"