0

yii2如何实现自定义验证?

我在模型规则中的代码是

public function rules()
{
    return [
        [['product_price'], 'checkMaxPrice']
    ];
}

public function checkMaxPrice($attribute,$params)
{
    if($this->product_price > 1000) {
        $this->addError($attribute,'Price must be less than 1000');
    }
}

我还有什么需要考虑的吗?

4

2 回答 2

2

将您的规则属性更改为:

public function rules()
{
    return [
        [['product_price'], 'checkMaxPrice' ,'skipOnEmpty' => false]
    ];
}

要知道跳过空

于 2016-07-20T06:16:21.283 回答
0

模型中的一切看起来都不错。可以试试

echo $model->getErrors();

在您的控制器中。也许可以帮助您。

于 2016-07-21T14:29:30.860 回答