Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.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
YII2规则中的自定义验证无效_Yii_Yii2 - Fatal编程技术网

YII2规则中的自定义验证无效

YII2规则中的自定义验证无效,yii,yii2,Yii,Yii2,我已经在我的模型中这样做了: public function rules() { return [ [['category_name'], 'required','message' => 'Please enter {attribute}.'], [['category_name'], 'string', 'max' => 45], [['category_name'], 'safe', 'on' =>

我已经在我的模型中这样做了:

 public function rules() {
    return [         
        [['category_name'], 'required','message' => 'Please enter {attribute}.'],
        [['category_name'], 'string', 'max' => 45],
        [['category_name'], 'safe', 'on' => 'search'],
        ['category_name', 'checkName'],
    ];
}

public function checkName($attribute, $params) { 
       $model = Categories::find()->where('category_name = "' . $this->$attribute . '" AND status != ' . Categories::STATUS_DELETED)->all(); //STATUS_DELETED = 2 constant.
       if (count($model) > 0) {
            $this->addError($attribute,  'Category name is already exists.');
        }

}

问题在于,输入重复值时,checkName函数未触发。可能的原因是什么?

好的,有一个小问题,实际上表名是categories,模型只是categories,因此将模型名更改为categories解决了问题。

是否通过在categories::STATUS_DELETED->all;中正确关闭来检查?是的,它已正确关闭。您只需使用唯一验证程序即可