This is my first time use of yii framework and try to use kratik widget in view. In _form.php,
$data = [
"a" => "Apple",
"b" => "Banana",
"c" => "coconut",
];
$form->field($model, 'tag[]')->widget(Select2::classname(), [
'data' => $data,
'options' => ['placeholder' => 'choose category', 'multiple' => true],
'pluginOptions' => [
'tags' => true,
'tokenSeparators' => [',', ' '],
'maximumInputLength' => 10
],
])->label('Select Category'); ?>
I would like to get selected data from controller. Here is my controller.php,
public function actionCreate()
{
$model = new ChildData();
if ($model->load(Yii::$app->request->post())) {
$userId = Yii::$app->user->getId();
$model->user_id= $userId;
$model->fruit=json_encode($model->tag);
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
So, In Child table, I set fruit attribute as varchar(255). After running code,I selected all tags of fruits data from kartik widget but it only saved '[]' in fruit attribute.
Thus, I would like to know how to take data from kartik select2 in controller.php? What type of data does kartik select2 return? and how to pass selected data to view.php? Please feel free to guide me about using kartik widgets. Thank you.