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