Yii2 dropdown multiple selected values
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!
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,
]);
}