Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Yii2:listbox的语法是否正确?_Php_Yii2 - Fatal编程技术网

Php Yii2:listbox的语法是否正确?

Php Yii2:listbox的语法是否正确?,php,yii2,Php,Yii2,我试图使用一个列表框,并使用下面的语法进行multi-select,但这不起作用 <?= $form->field($model, 'weekday')->listBox([ 'monday'=>'Monday', 'tuesday'=>'Tuesday', 'wednesday'=>'Wednesday', 'thursday'=>'Thursday', 'friday'=&

我试图使用一个列表框,并使用下面的语法进行multi-select,但这不起作用

<?= $form->field($model, 'weekday')->listBox([
       'monday'=>'Monday',
        'tuesday'=>'Tuesday',
        'wednesday'=>'Wednesday',
        'thursday'=>'Thursday',
        'friday'=>'Friday',
        'saturday'=>'Saturday',
        'sunday'=>'Sunday'],['multiple'=>true,'size'=>7])

            ?>
控制器代码:

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::weekday 1.在E:\wamp\www\HospitalErp\vendor\yiisoft\yii2\base\Component.php的第143行 134135136137138139140141142143144145146147149150151152

 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() 
其原因附加到模型规则中的
工作日
属性:

[['priority', 'weekday'], 'string', 'max' => 20],
['weekdays', 'validateWeekDays'],
由于
multiple=>true
选项,您正在接收的是一个数组,而不是字符串(即使是在一个选择的情况下)。它根本没有通过验证

Yii 2中没有内置的数组验证器

1)由于您需要多选,我建议您将
工作日
重命名为
工作日

2)我建议将工作日列表放在单独的方法中:

public static function getWeekdaysList()
{
    return [
        'monday' => 'Monday',
        'tuesday' => 'Tuesday',
        'wednesday' => 'Wednesday',
        'thursday' => 'Thursday',
        'friday' => 'Friday',
        'saturday' => 'Saturday',
        'sunday' => 'Sunday',
    ];
}
3)创建,例如:

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;
        }
    }
}
在中阅读有关内联验证器的更多信息

4)将其附加到模型规则中的
工作日

[['priority', 'weekday'], 'string', 'max' => 20],
['weekdays', 'validateWeekDays'],
如果您不想验证
工作日
,则应明确将其标记为安全属性,以便将其与其他属性一起大量分配:

['weekdays', 'safe'],
5)在视图中,您可以将代码简化为:

<?= $form->field($model, 'weekdays')->listBox(Appointment::getWeekdaysList(), [
    'multiple' => true,
    'size' => 7,
]) ?> 


还有一点,这是一个工作日,不包括周六和周日。更正确的形式是一周中的几天。

对其进行了测试。对我来说一切都很好。在当前场景中它是安全属性吗?显示与此属性(模型、控制器)相关的其余代码。也许你错过了大量的任务?嗨arogachev-包括模型和控制器的相关代码。嗨agrogachev-我修改了代码,包括将工作日重命名为工作日-我现在收到错误-
获取未知属性:app\models\Appointment::weekday
你还应该在数据库中更改它,例如,在迁移的帮助下。再次仔细检查,可能你错过了重命名。是的,我在数据库中更改了它,不是借助迁移,而是在phpmydmin中,甚至覆盖了model.php,然后添加了代码,仍然存在相同的问题。显示完整的堆栈跟踪,这样我们就可以看到它来自的行。或者在编辑器中进行全局搜索和替换,很可能旧名称的属性仍然存在。嗨,arogachev-我已经为错误添加了堆栈跟踪-我在模型中找不到任何weekday实例。