2021-08-11 15:13:35 +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
|
|
|
|
|
{
|
|
|
|
|
$param = $this->getParams();
|
|
|
|
|
if (empty($param) || !isset($param[$this->field])) {
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (empty($this->model)) {
|
|
|
|
|
return $this->addError('Model error.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$model = $this->model;
|
2021-12-07 16:43:19 +08:00
|
|
|
if (!$this->model->getIsNowExample()) {
|
2021-08-11 15:13:35 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
2021-10-09 16:48:19 +08:00
|
|
|
if ($model::query()->where([$this->field => $param[$this->field]])->exists()) {
|
2021-08-11 15:13:35 +08:00
|
|
|
return $this->addError('The :attribute \'' . $param[$this->field] . '\' is exists!');
|
|
|
|
|
}
|
|
|
|
|
return $this->isFail = TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|