Yii2:correct 列表框的语法?
Yii2:correct syntax for listbox?
我正在尝试使用列表框并对多 select 使用以下语法,但它不起作用。
<?= $form->field($model, 'weekday')->listBox([
'monday'=>'Monday',
'tuesday'=>'Tuesday',
'wednesday'=>'Wednesday',
'thursday'=>'Thursday',
'friday'=>'Friday',
'saturday'=>'Saturday',
'sunday'=>'Sunday'],['multiple'=>true,'size'=>7])
?>
我可以在列表框中多select,但它返回一个空值。如果我参加 multiple'=>true
部分,那么它会返回正确的值,但是我无法进行 multi-select.
我做错了什么?
谢谢。
预约中的相关代码model.php
public函数规则()
{
return [
[['appointment_date'], 'safe'],
[['priority', 'weekday'], 'string', 'max' => 20]
];
}
控制器代码:
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,
]);
}
}
在更新 aragachev 的答案中的建议代码后我得到的错误的堆栈跟踪
变得未知 属性:app\models\Appointment::工作日
1. 在 E:\wamp\www\HospitalErp\vendor\yiisoft\yii2\base\Component.php 中的第 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()
原因是 StringValidator 附加到模型规则中的 weekday
属性:
[['priority', 'weekday'], 'string', 'max' => 20],
由于 multiple => true
选项,您收到的是一个数组,而不是字符串(即使只有一个选择)。它根本没有通过验证。
Yii 2 中没有内置数组验证器。
1)因为你要多选我建议你把weekday
重命名为weekdays
.
2) 我建议将工作日列表放在单独的方法中:
public static function getWeekdaysList()
{
return [
'monday' => 'Monday',
'tuesday' => 'Tuesday',
'wednesday' => 'Wednesday',
'thursday' => 'Thursday',
'friday' => 'Friday',
'saturday' => 'Saturday',
'sunday' => 'Sunday',
];
}
3)创建inline validator,例如,像这样:
public function validateWeekdays ($attribute, $params)
{
$label = '«' . $this->getAttributeLabel($attribute) . '»';
// Checking if it's array first
if (is_array(!$this->$attribute)) {
$this->addError($attribute, "$label must be array.");
return;
}
$allowedWeekdays = array_keys(static::getWeekdaysList());
// Checking if every selection is in list of allowed values
foreach ($this->$attribute as $weekday)
{
if (!in_array($weekday, $allowedWeekdays)) {
$this->addError($attribute, "$label - $weekday is not in allowed list");
return;
}
}
}
阅读 offical guide 中有关内联验证器的更多信息。
4) 将其附加到模型规则中的 weekdays
:
['weekdays', 'validateWeekDays'],
如果您不想验证 weekDays
您应该将其显式标记为安全属性,以便为它大量分配其他属性:
['weekdays', 'safe'],
5) 在视图中,您可以将代码简化为:
<?= $form->field($model, 'weekdays')->listBox(Appointment::getWeekdaysList(), [
'multiple' => true,
'size' => 7,
]) ?>
还有一点要注意 - weekday 今天是工作日,周六和周日除外。更正确的形式是 星期几.
我正在尝试使用列表框并对多 select 使用以下语法,但它不起作用。
<?= $form->field($model, 'weekday')->listBox([
'monday'=>'Monday',
'tuesday'=>'Tuesday',
'wednesday'=>'Wednesday',
'thursday'=>'Thursday',
'friday'=>'Friday',
'saturday'=>'Saturday',
'sunday'=>'Sunday'],['multiple'=>true,'size'=>7])
?>
我可以在列表框中多select,但它返回一个空值。如果我参加 multiple'=>true
部分,那么它会返回正确的值,但是我无法进行 multi-select.
我做错了什么?
谢谢。
预约中的相关代码model.php
public函数规则() {
return [
[['appointment_date'], 'safe'],
[['priority', 'weekday'], 'string', 'max' => 20]
];
}
控制器代码:
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,
]);
}
}
在更新 aragachev 的答案中的建议代码后我得到的错误的堆栈跟踪
变得未知 属性:app\models\Appointment::工作日 1. 在 E:\wamp\www\HospitalErp\vendor\yiisoft\yii2\base\Component.php 中的第 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()
原因是 StringValidator 附加到模型规则中的 weekday
属性:
[['priority', 'weekday'], 'string', 'max' => 20],
由于 multiple => true
选项,您收到的是一个数组,而不是字符串(即使只有一个选择)。它根本没有通过验证。
Yii 2 中没有内置数组验证器。
1)因为你要多选我建议你把weekday
重命名为weekdays
.
2) 我建议将工作日列表放在单独的方法中:
public static function getWeekdaysList()
{
return [
'monday' => 'Monday',
'tuesday' => 'Tuesday',
'wednesday' => 'Wednesday',
'thursday' => 'Thursday',
'friday' => 'Friday',
'saturday' => 'Saturday',
'sunday' => 'Sunday',
];
}
3)创建inline validator,例如,像这样:
public function validateWeekdays ($attribute, $params)
{
$label = '«' . $this->getAttributeLabel($attribute) . '»';
// Checking if it's array first
if (is_array(!$this->$attribute)) {
$this->addError($attribute, "$label must be array.");
return;
}
$allowedWeekdays = array_keys(static::getWeekdaysList());
// Checking if every selection is in list of allowed values
foreach ($this->$attribute as $weekday)
{
if (!in_array($weekday, $allowedWeekdays)) {
$this->addError($attribute, "$label - $weekday is not in allowed list");
return;
}
}
}
阅读 offical guide 中有关内联验证器的更多信息。
4) 将其附加到模型规则中的 weekdays
:
['weekdays', 'validateWeekDays'],
如果您不想验证 weekDays
您应该将其显式标记为安全属性,以便为它大量分配其他属性:
['weekdays', 'safe'],
5) 在视图中,您可以将代码简化为:
<?= $form->field($model, 'weekdays')->listBox(Appointment::getWeekdaysList(), [
'multiple' => true,
'size' => 7,
]) ?>
还有一点要注意 - weekday 今天是工作日,周六和周日除外。更正确的形式是 星期几.