I am trying to use a listbox and using the following syntax for multi-select, which is not working.
<?= $form->field($model, 'weekday')->listBox([
'monday'=>'Monday',
'tuesday'=>'Tuesday',
'wednesday'=>'Wednesday',
'thursday'=>'Thursday',
'friday'=>'Friday',
'saturday'=>'Saturday',
'sunday'=>'Sunday'],['multiple'=>true,'size'=>7])
?>
I am able to multi-select in the list box, but it is returning a null value. If I am taking the part multiple'=>true then It is returning the correct value, but then I am not able to multi-select.
What I am doing wrong here?
Thanks.
relevant code in the appointment model.php
public function rules() {
return [
[['appointment_date'], 'safe'],
[['priority', 'weekday'], 'string', 'max' => 20]
];
}
controller code:
public function actionCreate()
{
$model = new Appointment();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
Stack trace for the error I am getting after update of the suggested code in the answer by aragachev
Getting unknown property: app\models\Appointment::weekday 1. in E:\wamp\www\HospitalErp\vendor\yiisoft\yii2\base\Component.php at line 143 134135136137138139140141142143144145146147148149150151152
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name)) {
return $behavior->$name;
}
}
}
if (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);//line 143
}
}
/**
* Sets the value of a component property.
* This method will check in the following order and act accordingly:
*
* - a property defined by a setter: set the property value
* - an event in the format of "on xyz": attach the handler to the event "xyz"
2. in E:\wamp\www\HospitalErp\vendor\yiisoft\yii2\db\BaseActiveRecord.php – yii\base\Component::__get('weekday') at line 247
3. in E:\wamp\www\HospitalErp\vendor\yiisoft\yii2\helpers\BaseArrayHelper.php – yii\db\BaseActiveRecord::__get('weekday') at line 190
4. in E:\wamp\www\HospitalErp\vendor\yiisoft\yii2\widgets\DetailView.php – yii\helpers\BaseArrayHelper::getValue(app\models\Appointment, 'weekday') at line 209
5. in E:\wamp\www\HospitalErp\vendor\yiisoft\yii2\widgets\DetailView.php – yii\widgets\DetailView::normalizeAttributes() at line 123
6. in E:\wamp\www\HospitalErp\vendor\yiisoft\yii2\base\Object.php – yii\widgets\DetailView::init()