7 Commits

Author SHA1 Message Date
as2252258 011e0cd9b8 1 2021-12-12 06:06:15 +08:00
as2252258 a7531432d4 改名 2021-12-07 16:43:19 +08:00
as2252258 76bd050ada 改名 2021-12-02 16:34:14 +08:00
as2252258 0e6a1b5ba2 改名 2021-12-02 16:33:46 +08:00
as2252258 10aa70eb03 改名 2021-12-02 16:31:41 +08:00
as2252258 b80833691e 改名 2021-12-02 16:29:54 +08:00
as2252258 4ac84b3edf 改名 2021-12-02 16:28:14 +08:00
4 changed files with 15 additions and 15 deletions
+7 -4
View File
@@ -66,12 +66,12 @@ abstract class BaseValidator
}
/**
* @throws Exception
* @return bool
* @throws Exception
*/
public function trigger(): bool
{
throw new Exception('Child Class must define method of trigger');
throw new Exception('Child Class must define method of trigger');
}
/**
@@ -83,11 +83,14 @@ abstract class BaseValidator
}
/**
* @param array $data
* @param array|null $data
* @return $this
*/
public function setParams(array $data): static
public function setParams(?array $data): static
{
if (is_null($data)) {
$data = [];
}
$this->params = $data;
return $this;
}
+1 -4
View File
@@ -20,10 +20,7 @@ class RequiredValidator extends BaseValidator
public function trigger(): bool
{
$param = $this->getParams();
if (is_numeric($param)) {
return true;
}
if (empty($param) || !isset($param[$this->field])) {
if (!is_array($param) || !isset($param[$this->field])) {
return $this->addError('The param :attribute not exists');
} else {
return true;
+1 -1
View File
@@ -30,7 +30,7 @@ class UniqueValidator extends BaseValidator
}
$model = $this->model;
if (!$this->model->getIsCreate()) {
if (!$this->model->getIsNowExample()) {
return true;
}
if ($model::query()->where([$this->field => $param[$this->field]])->exists()) {
+6 -6
View File
@@ -186,16 +186,16 @@ class Validator extends BaseValidator
return true;
}
foreach ($this->validators as $val) {
if ($this->check($val)) {
[$result, $validator] = $this->check($val);
if ($result === true) {
continue;
}
$isTrue = false;
if ($val instanceof BaseValidator) {
$this->addError($val->getError());
if ($validator instanceof BaseValidator) {
$this->addError($validator->getError());
}
break;
}
$this->validators = null;
$this->validators = [];
return !isset($isTrue);
}
@@ -208,7 +208,7 @@ class Validator extends BaseValidator
private function check(BaseValidator|array|Closure $val): mixed
{
if (is_callable($val, true)) {
return call_user_func($val, $this);
return [call_user_func($val, $this), $val];
}
$class = Kiri::getDi()->get($val['class']);
@@ -216,7 +216,7 @@ class Validator extends BaseValidator
Kiri::configure($class, $val);
return $class->trigger();
return [$class->trigger(), $class];
}
}