Answer the question
In order to leave comments, you need to log in
Why is data not saved to the database when using Select2?
Hello. I'm trying to use the select2 widget from kartik-v in my project.
There is a model:
<?php
namespace app\models;
use yii\db\ActiveRecord;
use Yii;
class Delivery extends ActiveRecord{
public static function tableName()
{
return 'delivery';
}
public function attributeLabels()
{
return [
'id' => 'ID',
'parent_id' => 'Parent ID',
'materials_electric' => 'Список рассылки для материалов электрической части',
'materials_mehanic' => 'Список рассылки для материалов механической части',
'design_electric' => 'Список рассылки для проектирования электрической части',
'design_mehanic' => 'Список рассылки для проектирование механической части',
'installing_electric' => 'Список рассылки для монтажа электрической части',
'installing_mehanic' => 'Список рассылки для монтажа механической части',
'building' => 'Список рассылки для производства',
];
}
public function rules()
{
return [
['parent_id', 'required'],
[['materials_electric','materials_mehanic','design_electric','design_mehanic','installing_electric','installing_mehanic','building'],'trim']
];
}
public function getProjects(){
return $this->hasOne(Projects::className(),['id'=>'parent_id']);
}
}
<?= $form->field($model, 'materials_electric')->widget(Select2::classname(), [
'data' => $data,
'options' => [
'placeholder' => ' ',
'multiple' => true
],
]) ?>
Answer the question
In order to leave comments, you need to log in
I think you have an array there, therefore, before saving (and possibly before validation), it must be cast to a string. Try adding to the model:
public function beforeValidate() {
if(is_array($this->materials_electric)){
$this->materials_electric = implode(",", $this->materials_electric);
}
return parent::beforeValidate();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question