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;
|
|
|
|
|
|
|
|
|
|
|
2023-12-18 15:01:09 +08:00
|
|
|
use Database\ModelInterface;
|
|
|
|
|
|
2022-01-09 14:01:11 +08:00
|
|
|
class UniqueValidator extends BaseValidator
|
|
|
|
|
{
|
|
|
|
|
|
2023-12-18 15:01:09 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var ModelInterface
|
|
|
|
|
*/
|
|
|
|
|
public ModelInterface $model;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-12-19 14:51:13 +08:00
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
public string $field;
|
|
|
|
|
|
|
|
|
|
/**
|
2023-12-18 15:01:09 +08:00
|
|
|
* @param mixed $value
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws
|
|
|
|
|
* 检查是否存在
|
|
|
|
|
*/
|
2023-12-19 14:51:13 +08:00
|
|
|
public function trigger(mixed $value): bool
|
2023-12-18 15:01:09 +08:00
|
|
|
{
|
2023-12-19 14:51:13 +08:00
|
|
|
return $this->model::query()->where([$this->field => $value])->exists() === false;
|
2023-12-18 15:01:09 +08:00
|
|
|
}
|
2022-01-09 14:01:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|