From 5f110403aaf3296f960fd5ec11ae6503aeca378f Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 18 Sep 2022 03:44:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- UniqueValidator.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/UniqueValidator.php b/UniqueValidator.php index 9cedb8b..41a1814 100644 --- a/UniqueValidator.php +++ b/UniqueValidator.php @@ -26,13 +26,18 @@ class UniqueValidator extends BaseValidator if (!$this->model->getIsNowExample()) { return true; } - return $this->_validator($this->field, function ($field, $params, $model) { - if (!isset($params[$field])) { - return true; + return $this->_validator($this->field, function ($_item, $params, $model) { + if (!is_array($_item)) { + $_item = [$_item]; } - $param = $params[$field]; - if ($model::query()->where([$field => $param])->exists()) { - return $this->addError($field,'The :attribute \'' . $param . '\' is exists!'); + $data = array_reduce($_item, function ($resp, $next) use ($params) { + $array = empty($resp) ? [] : $resp; + $array[$next] = $params[$next] ?? null; + return $array; + }); + if ($model::query()->where($data)->exists()) { + return $this->addError(implode(',', $_item), + 'The :attribute \'' . implode(',', $_item) . '\' is exists!'); } return $this->isFail = TRUE; }, $this->params, $this->model);