Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a7531432d4 | |||
| 76bd050ada | |||
| 0e6a1b5ba2 | |||
| 10aa70eb03 | |||
| b80833691e | |||
| 4ac84b3edf |
@@ -66,12 +66,12 @@ abstract class BaseValidator
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
|
||||||
* @return bool
|
* @return bool
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function trigger(): bool
|
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
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function setParams(array $data): static
|
public function setParams(?array $data): static
|
||||||
{
|
{
|
||||||
|
if (is_null($data)) {
|
||||||
|
$data = [];
|
||||||
|
}
|
||||||
$this->params = $data;
|
$this->params = $data;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,10 +20,7 @@ class RequiredValidator extends BaseValidator
|
|||||||
public function trigger(): bool
|
public function trigger(): bool
|
||||||
{
|
{
|
||||||
$param = $this->getParams();
|
$param = $this->getParams();
|
||||||
if (is_numeric($param)) {
|
if (!is_array($param) || !isset($param[$this->field])) {
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (empty($param) || !isset($param[$this->field])) {
|
|
||||||
return $this->addError('The param :attribute not exists');
|
return $this->addError('The param :attribute not exists');
|
||||||
} else {
|
} else {
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ class UniqueValidator extends BaseValidator
|
|||||||
}
|
}
|
||||||
|
|
||||||
$model = $this->model;
|
$model = $this->model;
|
||||||
if (!$this->model->getIsCreate()) {
|
if (!$this->model->getIsNowExample()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if ($model::query()->where([$this->field => $param[$this->field]])->exists()) {
|
if ($model::query()->where([$this->field => $param[$this->field]])->exists()) {
|
||||||
|
|||||||
+6
-5
@@ -186,12 +186,13 @@ class Validator extends BaseValidator
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
foreach ($this->validators as $val) {
|
foreach ($this->validators as $val) {
|
||||||
if ($this->check($val)) {
|
[$result, $validator] = $this->check($val);
|
||||||
|
if ($result === true) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$isTrue = false;
|
$isTrue = false;
|
||||||
if ($val instanceof BaseValidator) {
|
if ($validator instanceof BaseValidator) {
|
||||||
$this->addError($val->getError());
|
$this->addError($validator->getError());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -208,7 +209,7 @@ class Validator extends BaseValidator
|
|||||||
private function check(BaseValidator|array|Closure $val): mixed
|
private function check(BaseValidator|array|Closure $val): mixed
|
||||||
{
|
{
|
||||||
if (is_callable($val, true)) {
|
if (is_callable($val, true)) {
|
||||||
return call_user_func($val, $this);
|
return [call_user_func($val, $this), $val];
|
||||||
}
|
}
|
||||||
|
|
||||||
$class = Kiri::getDi()->get($val['class']);
|
$class = Kiri::getDi()->get($val['class']);
|
||||||
@@ -216,7 +217,7 @@ class Validator extends BaseValidator
|
|||||||
|
|
||||||
Kiri::configure($class, $val);
|
Kiri::configure($class, $val);
|
||||||
|
|
||||||
return $class->trigger();
|
return [$class->trigger(), $class];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user