7

I used Yii2 multiple selection dropdown , it is working fine at create but not showing me selected values on update!

Form:

       $form->field($model, 'categories[]')            
         ->dropDownList($model->CategoryDropdown,
         [
          'multiple'=>'multiple'
          'class'=>'chosen-select input-md required',              
         ]             
        )->label("Add Categories");    

Model:

public function getCategoryDropdown()
{
        $listCategory   = Category::find()->select('ID,name')
            ->where(['is_subcategory' => 'Yes'])
            ->andWhere(['status' => 'active','approved' => 'active'])
            ->all();
        $list   = ArrayHelper::map( $listCategory,'ID','name');

        return $list;
}

Controller:

 public function actionCreate(){
 ...
     $model->categories = implode(",",$_POST['Company']['categories']);
    ...
    return $this->render('create', [
            'model' => $model,           
        ]);
 }

public function actionUpdate($id)
{
    $model = $this->findModel($id);    

    echo $model->categories; //  1,2,4,5  values already assigned
    ...
    return $this->render('update', [
                'model' => $model,                    
            ]); 
  }

Database:

1,2,4,5

How I can show multi selected values in dropdown when I update my recored?

Muhammad Shahzad
  • 7,778
  • 20
  • 71
  • 123

3 Answers3

12

all your code is ok just need echo your $form->...

echo $form->field($model, 'categories[]')            
     ->dropDownList($model->CategoryDropdown,
     [
      'multiple'=>'multiple',
      'class'=>'chosen-select input-md required',              
     ]             
    )->label("Add Categories"); 

or use <?= ?> in view!

Deep Kakkar
  • 4,538
  • 4
  • 26
  • 58
mohsen
  • 971
  • 12
  • 39
2

Here is solution:

Form

 <?= 
  $form->field($model, 'categories')            
         ->dropDownList($model->CategoryDropdown,
         [
          'class'=>'chosen-select input-md required',
          'multiple'=>'multiple'              
         ]             
        )->label("Add Categories");
 ?>

Controller:

 public function actionCreate(){
 ...
     $model->categories = implode(",",$_POST['Company']['categories']);
    ...
    return $this->render('create', [
            'model' => $model,           
        ]);
 }

public function actionUpdate($id)
{
    $model = $this->findModel($id); 
    $model->categories = explode(',', $model->categories);        
    ...
    if($model->load(Yii::$app->request->post()))
    {
         $model->categories = implode(",",$_POST['Company']['categories']);    
         ...
         $model->save()
    }
    return $this->render('update', [
                'model' => $model,                    
            ]); 
  }
Muhammad Shahzad
  • 7,778
  • 20
  • 71
  • 123
  • *@Shahzad Bhai*, How To Show Selected Values In Dropdown (Which Is Multi Select). Do You Have Any Idea Or Any Link.? Means, Whatever Values I Selected From Multi Select Dropdown, I wanted Those Values While Updating Time. – Nana Partykar Jun 21 '16 at 10:08
  • @NanaPartykar I have used a single column in db like category and store values comma separated 4,3,6,5 and I have used bootstrap chosen-select in my form, and above code was working fine for me. – Muhammad Shahzad Jun 21 '16 at 16:52
1

I would highly recommend to use Select2 from krajee it has all the options that you might need, using multiple in dropDownList() will make you able to choose multiple values but by pressing and holding Ctrl and choosing which is not comfortable as choosing them using Select2.

They did an amazing job i highly recommend anyone with this problem to use it.

Please check the documentations. https://demos.krajee.com/widget-details/select2

WardNsour
  • 33
  • 8