2020-08-31 12:38:32 +08:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Created by PhpStorm.
|
|
|
|
|
* User: qv
|
|
|
|
|
* Date: 2018/10/16 0016
|
|
|
|
|
* Time: 10:24
|
|
|
|
|
*/
|
2020-10-29 18:17:25 +08:00
|
|
|
declare(strict_types=1);
|
2020-08-31 12:38:32 +08:00
|
|
|
|
|
|
|
|
namespace validator;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UniqueValidator extends BaseValidator
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws
|
|
|
|
|
* 检查是否存在
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function trigger(): bool
|
2020-08-31 12:38:32 +08:00
|
|
|
{
|
|
|
|
|
$param = $this->getParams();
|
|
|
|
|
if (empty($param) || !isset($param[$this->field])) {
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (empty($this->model)) {
|
|
|
|
|
return $this->addError('Model error.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$model = $this->model;
|
|
|
|
|
if (!$this->model->getIsCreate()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if ($model::find()->where([$this->field => $param[$this->field]])->exists()) {
|
|
|
|
|
return $this->addError('The :attribute \'' . $param[$this->field] . '\' is exists!');
|
|
|
|
|
}
|
|
|
|
|
return $this->isFail = TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|