8

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?

13
  • show the update action (controller) Commented Oct 29, 2015 at 6:30
  • no logic used in update action but I'm updating my question. Commented Oct 29, 2015 at 6:39
  • is there any option? to explode these values in dropdown? like: dropDownList($model, 'categories', ''explode(",",$model->categories), Commented Oct 29, 2015 at 6:45
  • You save the imploded selected categories in create. right? Commented Oct 29, 2015 at 6:51
  • 1
    I got the solution, just removed '[]' from $form->field($model, 'categories[]') replaced with $form->field($model, 'categories') , it is working now. Commented Oct 29, 2015 at 7:25

3 Answers 3

14

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!

Sign up to request clarification or add additional context in comments.

2 Comments

You should use $form->field($Item, 'categories')->listBox($dropdown_data);
I should need to use listBox.
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,                    
            ]); 
  }

2 Comments

@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.
@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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.