Answer the question
In order to leave comments, you need to log in
Yii2, activerecord - how to record and validate a select miltiple field?
1. Actually in the controller:
$model = new Model;
$model->load(Yii::$app->request->post());
if($model->validate()) {
$model->save();
}
<select name=Model['select'] multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
Answer the question
In order to leave comments, you need to log in
Good evening.
Try like this.
In the model:
public $select_list;
public function rules()
{
return [
['select_list', 'required'],
['select_list', 'each', 'rule' => ['integer']]
];
}
public static function getValuesArray()
{
return [
'first' => 'first',
'1' => 'second',
'2' => 'third'
];
}
For these kinds of fields, the MySQL SET type is usually used, which allows you to select multiple values. There is a behavior that automatically converts to and from an array:
https://github.com/mhthnz/yii2-helpful-behaviors#y...
Regarding your code:
public function rules()
{
return [
['select', 'required'],
['select', 'each', 'rule' => ['in', 'range' => ['volvo', 'saab', 'opel', 'audi']]],
];
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question