Yii2 在验证之前抛出 PHP 警告
Yii2 before Validate throw PHP Warning
我使用了 beforeValidate($insert)
函数,当我访问我的 post 列表页面时它抛出了 PHP 警告:
http://localhost/yiiapp/backend/web/index.php?r=post/index
PHP Warning – yii\base\ErrorException
Missing argument 1 for app\models\Post::beforeValidate(), called in /var/www/html/yiiapp/vendor/yiisoft/yii2/base/Model.php on line 341 and defined
但是当我访问我的创建页面时,这个异常消失了:
http://localhost/yiiapp/backend/web/index.php?r=post/create
实际上我想在 Post 模型中验证之前为我的属性 user_id
赋值。
这里是Post型号:
class Post extends \yii\db\ActiveRecord
{
public static function tableName()
{
return 'post';
}
public function beforeValidate($insert)
{
if (parent::beforeValidate($insert)) {
$this->user_id = Yii::$app->user->id;
return true;
}
return false;
}
---
}
为什么会出现这个异常?
我该如何解决这个问题?
根据文档 http://www.yiiframework.com/doc-2.0/yii-base-model.html#beforeValidate()-detail 方法 beforeValidate 没有属性。这样使用:
public function beforeValidate()
{
if (parent::beforeValidate()) {
return true;
}
return false;
}
我使用了 beforeValidate($insert)
函数,当我访问我的 post 列表页面时它抛出了 PHP 警告:
http://localhost/yiiapp/backend/web/index.php?r=post/index
PHP Warning – yii\base\ErrorException
Missing argument 1 for app\models\Post::beforeValidate(), called in /var/www/html/yiiapp/vendor/yiisoft/yii2/base/Model.php on line 341 and defined
但是当我访问我的创建页面时,这个异常消失了:
http://localhost/yiiapp/backend/web/index.php?r=post/create
实际上我想在 Post 模型中验证之前为我的属性 user_id
赋值。
这里是Post型号:
class Post extends \yii\db\ActiveRecord
{
public static function tableName()
{
return 'post';
}
public function beforeValidate($insert)
{
if (parent::beforeValidate($insert)) {
$this->user_id = Yii::$app->user->id;
return true;
}
return false;
}
---
}
为什么会出现这个异常?
我该如何解决这个问题?
根据文档 http://www.yiiframework.com/doc-2.0/yii-base-model.html#beforeValidate()-detail 方法 beforeValidate 没有属性。这样使用:
public function beforeValidate()
{
if (parent::beforeValidate()) {
return true;
}
return false;
}