🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
Validate 有很大概率会被继承所以这里列出protected方法和属性 **protected属性:** $type = [] -- 自定义验证类型 $alias = [ '>' => 'gt', '>=' => 'egt', '<' => 'lt', '<=' => 'elt', '=' => 'eq', 'same' => 'eq',] -- 验证类型别名 $rule = [] -- 当前验证规则 $message = [] -- 验证提示信息 $field = [] -- 验证字段描述 $currentScene="" -- 当前验证场景 $scene = [] -- 验证场景定义 $error = [] 或 $error ="" -- 验证失败错误信息 $batch = false -- 是否批量验证 $failException = false -- 验证失败是否抛出异常 $only = [] -- 场景需要验证的规则 $remove = [] -- 场景需要移除的验证规则 $append = [] -- 场景需要追加的验证规则 $regex = [] -- 验证正则定义 $db -- Db对象 $lang -- 语言对象 $request -- Request请求对象 $maker = [] -- $typeMsg -- 默认规则提示 ``` $typeMsg = [ 'require' => ':attribute require', 'must' => ':attribute must', 'number' => ':attribute must be numeric', 'integer' => ':attribute must be integer', 'float' => ':attribute must be float', 'boolean' => ':attribute must be bool', 'email' => ':attribute not a valid email address', 'mobile' => ':attribute not a valid mobile', 'array' => ':attribute must be a array', 'accepted' => ':attribute must be yes,on or 1', 'date' => ':attribute not a valid datetime', 'file' => ':attribute not a valid file', 'image' => ':attribute not a valid image', 'alpha' => ':attribute must be alpha', 'alphaNum' => ':attribute must be alpha-numeric', 'alphaDash' => ':attribute must be alpha-numeric, dash, underscore', 'activeUrl' => ':attribute not a valid domain or ip', 'chs' => ':attribute must be chinese', 'chsAlpha' => ':attribute must be chinese or alpha', 'chsAlphaNum' => ':attribute must be chinese,alpha-numeric', 'chsDash' => ':attribute must be chinese,alpha-numeric,underscore, dash', 'url' => ':attribute not a valid url', 'ip' => ':attribute not a valid ip', 'dateFormat' => ':attribute must be dateFormat of :rule', 'in' => ':attribute must be in :rule', 'notIn' => ':attribute be notin :rule', 'between' => ':attribute must between :1 - :2', 'notBetween' => ':attribute not between :1 - :2', 'length' => 'size of :attribute must be :rule', 'max' => 'max size of :attribute must be :rule', 'min' => 'min size of :attribute must be :rule', 'after' => ':attribute cannot be less than :rule', 'before' => ':attribute cannot exceed :rule', 'expire' => ':attribute not within :rule', 'allowIp' => 'access IP is not allowed', 'denyIp' => 'access IP denied', 'confirm' => ':attribute out of accord with :2', 'different' => ':attribute cannot be same with :2', 'egt' => ':attribute must greater than or equal :rule', 'gt' => ':attribute must greater than :rule', 'elt' => ':attribute must less than or equal :rule', 'lt' => ':attribute must less than :rule', 'eq' => ':attribute must equal :rule', 'unique' => ':attribute has exists', 'regex' => ':attribute not conform to the rules', 'method' => 'invalid Request method', 'token' => 'invalid token', 'fileSize' => 'filesize not match', 'fileExt' => 'extensions to upload is not allowed', 'fileMime' => 'mimetype to upload is not allowed', ]; ``` $defaultRegex -- 内置正则验证规则 ``` $defaultRegex = [ 'alpha' => '/^[A-Za-z]+$/', 'alphaNum' => '/^[A-Za-z0-9]+$/', 'alphaDash' => '/^[A-Za-z0-9\-\_]+$/', 'chs' => '/^[\x{4e00}-\x{9fa5}]+$/u', 'chsAlpha' => '/^[\x{4e00}-\x{9fa5}a-zA-Z]+$/u', 'chsAlphaNum' => '/^[\x{4e00}-\x{9fa5}a-zA-Z0-9]+$/u', 'chsDash' => '/^[\x{4e00}-\x{9fa5}a-zA-Z0-9\_\-]+$/u', 'mobile' => '/^1[3-9]\d{9}$/', 'idCard' => '/(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)/', 'zip' => '/\d{6}/', ]; ``` $filter -- Filter_var 规则 ``` $filter = [ 'email' => FILTER_VALIDATE_EMAIL, 'ip' => [FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6], 'integer' => FILTER_VALIDATE_INT, 'url' => FILTER_VALIDATE_URL, 'macAddr' => FILTER_VALIDATE_MAC, 'float' => FILTER_VALIDATE_FLOAT, ]; ``` **public方法:** static maker(Closure $maker):void -- 设置服务注入 setLang(Lang $lang):void -- 设置Lang对象 setDb(Db $db):void -- 设置Db对象 setRequest(Request $request):void -- 设置Request对象 rule($name, $rule = ''):Validate -- 添加字段验证规则 extend(string $type, callable $callback = null, string $message = null):Validate -- 添加字段验证规则 setTypeMsg($type, string $msg = null): void -- 设置验证规则的默认提示信息 message(array $message):Validate -- 设置提示信息 scene(string $name):Validate -- 设置验证场景 hasScene(string $name): bool -- 判断是否存在某个验证场景 batch(bool $batch = true):Validate -- 设置批量验证 failException(bool $fail = true):Validate -- 设置验证失败后是否抛出异常 only(array $fields):Validate -- 指定需要验证的字段列表 remove($field, $rule = null):Validate -- 移除某个字段的验证规则 append($field, $rule = null):Validate -- 追加某个字段的验证规则 check(array $data, array $rules = []): bool -- 数据自动验证 checkRule($value, $rules): bool -- 根据验证规则验证数据 confirm($value, $rule, array $data = [], string $field = ''): bool -- 验证是否和某个字段的值一致 different($value, $rule, array $data = []): bool -- 验证是否和某个字段的值是否不同 egt($value, $rule, array $data = []): bool -- 验证是否大于等于某个值 gt($value, $rule, array $data = []): bool -- 验证是否大于某个值 elt($value, $rule, array $data = []): bool -- 验证是否小于等于某个值 lt($value, $rule, array $data = []): bool -- 验证是否小于某个值 eq($value, $rule): bool -- 验证是否等于某个值 must($value, $rule = null): bool -- 必须验证 is($value, string $rule, array $data = []): bool -- 验证字段值是否为有效格式 token($value, string $rule, array $data): bool -- 验证表单令牌 activeUrl(string $value, string $rule = 'MX'): bool -- 验证是否为合格的域名或者IP 支持A,MX,NS,SOA,PTR,CNAME,AAAA,A6, SRV,NAPTR,TXT 或者 ANY类型 ip($value, string $rule = 'ipv4'): bool -- 验证是否有效IP fileExt($file, $rule): bool -- 验证上传文件后缀 fileMime($file, $rule): bool -- 验证上传文件类型 fileSize($file, $rule): bool -- 验证上传文件大小 image($file, $rule): bool -- 验证图片的宽高及类型 dateFormat($value, $rule): bool -- 验证时间和日期是否符合指定格式 unique($value, $rule, array $data = [], string $field = ''): bool -- 验证是否唯一 filter($value, $rule): bool -- 使用filter_var方式验证 requireIf($value, $rule, array $data = []): bool -- 验证某个字段等于某个值的时候必须 requireCallback($value, $rule, array $data = []): bool -- 通过回调方法验证某个字段是否必须 requireWith($value, $rule, array $data = []): bool -- 验证某个字段有值的情况下必须 requireWithout($value, $rule, array $data = []): bool -- 验证某个字段没有值的情况下必须 in($value, $rule): bool -- 验证是否在范围内 notIn($value, $rule): bool -- 验证是否不在某个范围 between($value, $rule): bool -- between验证数据 notBetween($value, $rule): bool -- 使用notbetween验证数据 length($value, $rule): bool -- 验证数据长度 max($value, $rule): bool -- 验证数据最大长度 min($value, $rule): bool -- 验证数据最小长度 after($value, $rule, array $data = []): bool -- 验证日期 before($value, $rule, array $data = []): bool -- 验证日期 afterWith($value, $rule, array $data = []): bool -- 验证日期 beforeWith($value, $rule, array $data = []): booll -- 验证日期 expire($value, $rule): bool -- 验证有效期 allowIp($value, $rule): bool -- 验证IP许可 denyIp($value, $rule): bool -- 验证IP禁用 regex($value, $rule): bool -- 使用正则验证数据 getError():array|string -- 获取错误信息 __call($method, $args):bool -- 动态方法 直接调用is方法进行验证 **protected方法:** checkItem(string $field, $value, $rules, $data, string $title = '', array $msg = []):mixed -- 验证单个字段规则 getValidateType($key, $rule): array -- 获取当前验证类型及规则 getImageType($image):string|false -- 判断图像类型 checkExt(File $file, $ext): bool -- 检测上传文件后缀 checkSize(File $file, $size): bool -- 检测上传文件大小 checkMime(File $file, $mime): bool -- 检测上传文件类型 getDataValue(array $data, $key) -- 获取数据值 getRuleMsg(string $attribute, string $title, string $type, $rule):string|array -- 获取验证规则的错误提示信息 parseErrorMsg(string $msg, $rule, string $title):string -- 获取验证规则的错误提示信息 errorMsgIsArray(array $msg, $rule, string $title):array -- 错误信息数组处理 getScene(string $scene): void -- 获取数据验证的场景