This commit is contained in:
2022-09-18 03:44:51 +08:00
parent c8194f3089
commit 5f110403aa
+11 -6
View File
@@ -26,13 +26,18 @@ class UniqueValidator extends BaseValidator
if (!$this->model->getIsNowExample()) { if (!$this->model->getIsNowExample()) {
return true; return true;
} }
return $this->_validator($this->field, function ($field, $params, $model) { return $this->_validator($this->field, function ($_item, $params, $model) {
if (!isset($params[$field])) { if (!is_array($_item)) {
return true; $_item = [$_item];
} }
$param = $params[$field]; $data = array_reduce($_item, function ($resp, $next) use ($params) {
if ($model::query()->where([$field => $param])->exists()) { $array = empty($resp) ? [] : $resp;
return $this->addError($field,'The :attribute \'' . $param . '\' is exists!'); $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; return $this->isFail = TRUE;
}, $this->params, $this->model); }, $this->params, $this->model);