我在我的模型中做到了这一点:
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函数。可能的原因是什么?
答案 0 :(得分:0)
确定存在一个小问题,实际上表名是类别,而模型只是类别,所以将模型名称更改为类别解决了问题。