Files
kiri-validator/UniqueValidator.php
T
as2252258 84c345375b Revert "改名"
This reverts commit fdf58326
2022-01-08 18:49:06 +08:00

42 lines
748 B
PHP

<?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.');
}
if (!$this->model->getIsNowExample()) {
return true;
}
if ($this->model::query()->where([$this->field => $param[$this->field]])->exists()) {
return $this->addError('The :attribute \'' . $param[$this->field] . '\' is exists!');
}
return $this->isFail = TRUE;
}
}