diff --git a/ArrayValidator.php b/ArrayValidator.php index a2ef072..040a4d4 100644 --- a/ArrayValidator.php +++ b/ArrayValidator.php @@ -30,7 +30,7 @@ class ArrayValidator extends BaseValidator return true; } if (!is_array($value)) { - return $this->addError('The param :attribute must a array'); + return $this->addError($field, 'The param :attribute must a array'); } return true; }, $this->params); diff --git a/BaseValidator.php b/BaseValidator.php index da1be48..7bf7702 100644 --- a/BaseValidator.php +++ b/BaseValidator.php @@ -99,11 +99,11 @@ abstract class BaseValidator * @param $message * @return bool */ - public function addError($message): bool + public function addError($field, $message): bool { $this->isFail = FALSE; - $message = str_replace(':attribute', $this->field, $message); + $message = str_replace(':attribute', $field, $message); $this->message = $message; diff --git a/DateTimeValidator.php b/DateTimeValidator.php index 735cf22..310dd6f 100644 --- a/DateTimeValidator.php +++ b/DateTimeValidator.php @@ -31,10 +31,10 @@ class DateTimeValidator extends BaseValidator return true; } return match ($method) { - self::DATE => $this->validatorDate($value), - self::DATE_TIME => $this->validateDatetime($value), - self::TIME => $this->validatorTime($value), - self::STR_TO_TIME => $this->validatorTimestamp($value), + self::DATE => $this->validatorDate($field, $value), + self::DATE_TIME => $this->validateDatetime($field, $value), + self::TIME => $this->validatorTime($field, $value), + self::STR_TO_TIME => $this->validatorTimestamp($field, $value), default => true, }; }, $this->params, strtolower($this->method)); @@ -48,16 +48,16 @@ class DateTimeValidator extends BaseValidator * * 效验分秒 格式如 01:02 or 01-02 */ - public function validatorTime($value): bool + public function validatorTime($field, $value): bool { if (!is_string($value)) { - return $this->addError('The param :attribute not is a date value'); + return $this->addError($field, 'The param :attribute not is a date value'); } $match = preg_match('/^[0-5]?\d{1}.{1}[0-5]?\d{1}$/', $value, $result); if ($match && $result[0] == $value) { return true; } else { - return $this->addError('The param :attribute format error'); + return $this->addError($field, 'The param :attribute format error'); } } @@ -68,17 +68,17 @@ class DateTimeValidator extends BaseValidator * * 效验分秒 格式如 2017-12-22 01:02 */ - public function validateDatetime($value): bool + public function validateDatetime($field, $value): bool { if (!is_string($value)) { - return $this->addError('The param :attribute not is a date value'); + return $this->addError($field, 'The param :attribute not is a date value'); } $match = '/^\d{4}\-\d{2}\-\d{2}\s+\d{2}:\d{2}:\d{2}$/'; $match = preg_match($match, $value, $result); if ($match && $result[0] == $value) { return true; } else { - return $this->addError('The param :attribute format error'); + return $this->addError($field, 'The param :attribute format error'); } } @@ -88,16 +88,16 @@ class DateTimeValidator extends BaseValidator * * 效验分秒 格式如 2017-12-22 */ - public function validatorDate($value): bool + public function validatorDate($field, $value): bool { if (!is_string($value)) { - return $this->addError('The param :attribute not is a date value'); + return $this->addError($field, 'The param :attribute not is a date value'); } $match = preg_match('/^(\d{4}).*([0-12]).*([0-31]).*$/', $value, $result); if ($match && $result[0] == $value) { return true; } else { - return $this->addError('The param :attribute format error'); + return $this->addError($field, 'The param :attribute format error'); } } @@ -107,16 +107,16 @@ class DateTimeValidator extends BaseValidator * * 效验时间戳 格式如 1521452254 */ - public function validatorTimestamp($value): bool + public function validatorTimestamp($field, $value): bool { if (!is_numeric($value)) { - return $this->addError('The param :attribute not is a timestamp value'); + return $this->addError($field, 'The param :attribute not is a timestamp value'); } if (strlen((string)$value) != 10) { - return $this->addError('The param :attribute not is a timestamp value'); + return $this->addError($field, 'The param :attribute not is a timestamp value'); } if (!date('YmdHis', $value)) { - return $this->addError('The param :attribute format error'); + return $this->addError($field, 'The param :attribute format error'); } return true; } diff --git a/EmailValidator.php b/EmailValidator.php index 842b500..445c6e4 100644 --- a/EmailValidator.php +++ b/EmailValidator.php @@ -26,13 +26,13 @@ class EmailValidator extends BaseValidator } $exp = "^[a-z\'0-9]+([._-][a-z\'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$"; if (!preg_match($exp, $value)) { - return $this->addError('The param :attribute format error'); + return $this->addError($field,'The param :attribute format error'); } [$account, $domain] = explode("@", $value); if (checkdnsrr($domain, "MX")) { return true; } - return $this->addError('The param :attribute format error'); + return $this->addError($field,'The param :attribute format error'); }, $this->params); } diff --git a/EmptyValidator.php b/EmptyValidator.php index f54aa67..096bb2c 100644 --- a/EmptyValidator.php +++ b/EmptyValidator.php @@ -33,11 +33,11 @@ class EmptyValidator extends BaseValidator return $this->_validator($this->field, function ($field, $params, $method) { $value = $params[$field] ?? null; if (empty($value)) { - return $this->addError(':attribute not exists'); + return $this->addError($field,':attribute not exists'); } return match ($method) { - self::CAN_NOT_EMPTY => isset($value[1]) || $this->addError('The :attribute can not empty.'), - default => $value !== null || $this->addError('The :attribute can not empty.') + self::CAN_NOT_EMPTY => isset($value[1]) || $this->addError($field,'The :attribute can not empty.'), + default => $value !== null || $this->addError($field,'The :attribute can not empty.') }; }, $this->params, strtolower($this->method)); } diff --git a/EnumValidator.php b/EnumValidator.php index a470265..0de1348 100644 --- a/EnumValidator.php +++ b/EnumValidator.php @@ -22,10 +22,10 @@ class EnumValidator extends BaseValidator return $this->_validator($this->field, function ($field, $params, $values) { $value = $params[$field] ?? null; if (empty($value)) { - return $this->addError('The param :attribute is null'); + return $this->addError($field,'The param :attribute is null'); } if (!in_array($value, $values)) { - return $this->addError('The param :attribute value only in ' . implode(',', $values)); + return $this->addError($field,'The param :attribute value only in ' . implode(',', $values)); } return true; }, $this->params, $this->value); diff --git a/IntegerValidator.php b/IntegerValidator.php index de03a65..7686687 100644 --- a/IntegerValidator.php +++ b/IntegerValidator.php @@ -30,10 +30,10 @@ class IntegerValidator extends BaseValidator return true; } if ($type !== self::MIN && $value < $origin) { - return $this->addError('The ' . $field . ' cannot be less than the default value.'); + return $this->addError($field,'The ' . $field . ' cannot be less than the default value.'); } if ($type !== self::MAX && $value > $origin) { - return $this->addError('The ' . $field . ' cannot be greater than the default value.'); + return $this->addError($field,'The ' . $field . ' cannot be greater than the default value.'); } return true; }, $this->params, $this->value, $this->type); diff --git a/LengthValidator.php b/LengthValidator.php index c10d82c..eb959af 100644 --- a/LengthValidator.php +++ b/LengthValidator.php @@ -29,15 +29,15 @@ class LengthValidator extends BaseValidator $value = $params[$this->field] ?? null; if (empty($value)) { if ($method != self::MAX_LENGTH) { - return $this->addError('The param :attribute not exists'); + return $this->addError($field, 'The param :attribute not exists'); } else { return TRUE; } } return match ($method) { - self::MAX_LENGTH => $this->maxLength($value), - self::MIN_LENGTH => $this->minLength($value), - default => $this->defaultLength($value), + self::MAX_LENGTH => $this->maxLength($field, $value), + self::MIN_LENGTH => $this->minLength($field, $value), + default => $this->defaultLength($field, $value), }; }, $this->params, strtolower($this->method), $this->value); } @@ -48,18 +48,18 @@ class LengthValidator extends BaseValidator * * 效验长度是否大于最大长度 */ - private function maxLength($value): bool + private function maxLength($field, $value): bool { if (is_array($value)) { if (count($value) > $value) { - return $this->addError('The param :attribute length overflow'); + return $this->addError($field, 'The param :attribute length overflow'); } } else { if (is_numeric($value) && strlen((string)$value) > $this->value) { - return $this->addError('The param :attribute length overflow'); + return $this->addError($field, 'The param :attribute length overflow'); } if (strlen($value) > $this->value) { - return $this->addError('The param :attribute length overflow'); + return $this->addError($field, 'The param :attribute length overflow'); } } return TRUE; @@ -71,18 +71,18 @@ class LengthValidator extends BaseValidator * * 效验长度是否小于最小长度 */ - private function minLength($value): bool + private function minLength($field, $value): bool { if (is_array($value)) { if (count($value) < $value) { - return $this->addError('The param :attribute length error'); + return $this->addError($field, 'The param :attribute length error'); } } else { if (is_numeric($value) && strlen((string)$value) < $this->value) { - return $this->addError('The param :attribute length overflow'); + return $this->addError($field, 'The param :attribute length overflow'); } if (strlen($value) < $this->value) { - return $this->addError('The param :attribute length error'); + return $this->addError($field, 'The param :attribute length error'); } } return TRUE; @@ -94,18 +94,18 @@ class LengthValidator extends BaseValidator * * 效验长度是否小于最小长度 */ - private function defaultLength($value): bool + private function defaultLength($field, $value): bool { if (is_array($value)) { if (count($value) !== $value) { - return $this->addError('The param :attribute length error'); + return $this->addError($field, 'The param :attribute length error'); } } else { if (is_numeric($value) && strlen((string)$value) !== $this->value) { - return $this->addError('The param :attribute length overflow'); + return $this->addError($field, 'The param :attribute length overflow'); } if (mb_strlen($value) !== $this->value) { - return $this->addError('The param :attribute length error; ' . mb_strlen($value) . ':' . $this->value); + return $this->addError($field, 'The param :attribute length error; ' . mb_strlen($value) . ':' . $this->value); } } return TRUE; diff --git a/RequiredValidator.php b/RequiredValidator.php index 4832cbc..b2f8f5d 100644 --- a/RequiredValidator.php +++ b/RequiredValidator.php @@ -21,7 +21,7 @@ class RequiredValidator extends BaseValidator { return $this->_validator($this->field, function ($field, $params) { if (!isset($params[$field])) { - return $this->addError('The param :attribute not exists'); + return $this->addError($field,'The param :attribute not exists'); } else { return true; } diff --git a/RoundValidator.php b/RoundValidator.php index bde6af0..a049960 100644 --- a/RoundValidator.php +++ b/RoundValidator.php @@ -26,7 +26,7 @@ class RoundValidator extends BaseValidator return $this->_validator($this->field, function ($field, $model, $param) { $value = $model->getAttribute($field); if ($value == null || round($value, $param) != $value) { - return $this->addError('The param :attribute length error'); + return $this->addError($field,'The param :attribute length error'); } return true; }, $this->model, $this->value); diff --git a/TypesOfValidator.php b/TypesOfValidator.php index 446bbc8..827d077 100644 --- a/TypesOfValidator.php +++ b/TypesOfValidator.php @@ -52,7 +52,7 @@ class TypesOfValidator extends BaseValidator } $value = $params[$field] ?? null; if (empty($value)) { - return $this->addError('This ' . $field . ' is not an empty data.'); + return $this->addError($field, 'This ' . $field . ' is not an empty data.'); } return $this->{$method . 'Format'}($field, $value); }, $this->params, $this->method, $this->types); @@ -66,10 +66,10 @@ class TypesOfValidator extends BaseValidator public function jsonFormat($field, $value): bool { if (!is_string($value) || is_numeric($value)) { - return $this->addError('The ' . $field . ' not is JSON data.'); + return $this->addError($field, 'The ' . $field . ' not is JSON data.'); } if (is_null(json_decode($value))) { - return $this->addError('The ' . $field . ' not is JSON data.'); + return $this->addError($field, 'The ' . $field . ' not is JSON data.'); } return true; } @@ -82,10 +82,10 @@ class TypesOfValidator extends BaseValidator public function serializeFormat($field, $value): bool { if (!is_string($value) || is_numeric($value)) { - return $this->addError('The ' . $field . ' not is serialize data.'); + return $this->addError($field, 'The ' . $field . ' not is serialize data.'); } if (false === swoole_unserialize($value)) { - return $this->addError('The ' . $field . ' not is serialize data.'); + return $this->addError($field, 'The ' . $field . ' not is serialize data.'); } return true; } @@ -98,7 +98,7 @@ class TypesOfValidator extends BaseValidator public function arrayFormat($field, $value): bool { if (!is_array($value)) { - return $this->addError('The ' . $field . ' not is array data.'); + return $this->addError($field, 'The ' . $field . ' not is array data.'); } return true; } @@ -111,7 +111,7 @@ class TypesOfValidator extends BaseValidator public function stringFormat($field, $value): bool { if (is_array($value) || is_object($value) || is_bool($value)) { - return $this->addError('The ' . $field . ' not is string data.'); + return $this->addError($field, 'The ' . $field . ' not is string data.'); } return true; } @@ -124,10 +124,10 @@ class TypesOfValidator extends BaseValidator public function integerFormat($field, $value): bool { if (!is_numeric($value)) { - return $this->addError('The ' . $field . ' not is number data.'); + return $this->addError($field, 'The ' . $field . ' not is number data.'); } if ((int)$value != $value) { - return $this->addError('The ' . $field . ' not is number data.'); + return $this->addError($field, 'The ' . $field . ' not is number data.'); } return true; @@ -142,7 +142,7 @@ class TypesOfValidator extends BaseValidator { $trim = (float)$value; if ($trim != $value) { - return $this->addError('The ' . $field . ' not is float data.'); + return $this->addError($field, 'The ' . $field . ' not is float data.'); } return true; } diff --git a/UniqueValidator.php b/UniqueValidator.php index e65d2a0..419c3fd 100644 --- a/UniqueValidator.php +++ b/UniqueValidator.php @@ -21,7 +21,7 @@ class UniqueValidator extends BaseValidator public function trigger(): bool { if (empty($model)) { - return $this->addError('Model error.'); + return $this->addError('model','Model error.'); } if (!$model->getIsNowExample()) { return true; @@ -32,7 +32,7 @@ class UniqueValidator extends BaseValidator } $param = $params[$field]; if ($model::query()->where([$field => $param])->exists()) { - return $this->addError('The :attribute \'' . $param . '\' is exists!'); + return $this->addError($field,'The :attribute \'' . $param . '\' is exists!'); } return $this->isFail = TRUE; }, $this->params, $this->model); diff --git a/Validator.php b/Validator.php index bf17232..8c7c78d 100644 --- a/Validator.php +++ b/Validator.php @@ -190,7 +190,7 @@ class Validator extends BaseValidator } $isTrue = false; if ($validator instanceof BaseValidator) { - $this->addError($validator->getError()); + $this->addError(null, $validator->getError()); } break; }