格式验证类
require/::isRequire #不得为空
number/::isNumber #验证字段是否为纯数字
integer/::isInteger #验证字段是否为整数
float/::isFloat #验证字段是否为浮点数
boolean/::isBoolean #验证字段是否为布尔值
email/::isEmail #验证字段是否为邮箱格式
array/isArray #验证字段是否为数组
accepted/isAccepted #验证字段是否为yes/on
date/isDate #验证字段是否为有效日期
alpha/isAlpha #验证字段是否为纯字母
alphaNum/isAlphaNum #验证字段是否为纯字母数字组合
alphaDash/isAlphaDash #验证字段是否为字母数字下划线破折号组好
chs/isChs #验证字段是否为汉字
chsAlpha/isChsAlpha #验证字段是否为汉字字母
chsAlphaNum/isChsAlphaNum #验证字段是否为汉字字母和数字
chsDash/isChsDash #验证字段是否为字母数字下划线破折号组好和汉字
cntrl/isCntrl #验证字段是否为控制字符(空格、缩进)
graph/isGraph #验证字段是否为可打印字符(不包括空格)
print/isPrint #验证字段是否为可打印字符(包括空格)
lower/isLower #验证字段是否为小写
upper/isUpper #验证字段是否为大写
space/isSpace #验证字段是否为空白字符
xdigit/isXdigit #验证字段是否为十六进制
activeUrl/isActiveUrl #验证字段是否为有效域名
url/isUrl #验证字段是否为有效URL地址
ip/isIp #验证字段是否为有效IP地址
deteFormat:format #验证字段的日期时间格式
mobile #验证字段是否为有效手机号
idCard #验证身份证格式
macAddr #验证MAC地址格式
zip #验证有效邮政编码
dateFormat:format #指定格式的日期。case :'at_date'=>'dateFormat:Y-m-d H:i:s'
长度和区间验证类
*注意 length,max, min 三个验证的共同特性
- 如果验证的数据是数组,则判断数组的长度。
- 如果验证的数据是File对象,则判断文件的大小。
in #范围,case:'num'=>'in:1,2,3,4'
notIn #不在某个范围。case:'num'=>'notIn:789
between #某个区间。case:'num'=>'between:1,20'
notBetween #不在某个范围。case:'num'=>'notBetween:2,5'
length:num1,num2 #长度是在某个范围 。case:'name'=>'length:6,18' ,指定长度 'name'=>'length:8'
max:number #最大长度。 case:'name'=>'max:18'
min:number #最小长度。case:'name'=>'min:6'
after:日期 #在某个日期之后。case:'begin_time' => 'after:2021-4-23',
before:日期 #在某个日期之前,例如:'end_time' => 'before:2028-10-01',
expire:开始时间,结束时间 #验证当前操作(不是某个值)是否在某个有效日期之间。case:'expire_time' => 'expire:2021-4-23,2028-10-01',
allowIp:allow1,allow2,... #验证当前请求的IP。case:'name' => 'allowIp:119.25.36.155',
denyIp:allow1,allow2,... #否禁止访问的IP。case:'name' => 'denyIp:114.45.4.55',
比较类
confirm #当前字段与其他字段比较是否一致。case:'repwd'=>'confirm:pwd'
different #当前字段与其他字段比较是否不一致。case:'user_name'=>'different:account'
egt 或者 >= #大于等于。case:'num'=>'egt:80'
gt 或者 > #大于。case:'num'=>'gt:90
elt 或者 <= #小于等于
lt 或者 < #小于
eq 、= 、same #等于。case:'num'=>'eq:100'、 'num'=>'=:100'、 'num'=>'same:100'
filter #使用filter_var方式验证。 case:'ip'=>'filter:strip_tags'
【点击详见filter_var】
正则验证
#case:'phone'=>'\d{11}'或者'phone'=>'regex:\d{11}'
上传验证
file #是否是一个上传文件
image:width,height,type #验证图片属性,width height和type都是可选,width和height必须同时定义。
fileExt:允许的文件后缀
fileMime:允许的文件类型
fileSize:允许的文件字节大小
行为验证
#case:'posData'=>'behavior:\app\index\behavior\Check'
其它特殊验证
unique:table,field,except,pk
验证字段值是否为唯一
验证规则 table,field,except,pk => 数据表,字段名,排除ID,主键名
// 表示验证name字段的值是否在user表(不包含前缀)中唯一
'name' => 'unique:user',
//验证其他字段
'name' => 'unique:user,account',
// 排除某个主键值
'name' => 'unique:user,account,10',
// 指定某个主键值排除
'name' =>'unique:user,account,10,user_id',
#*******复杂验证*********
// 多个字段验证唯一验证条件
'name' => 'unique:user,status^account',
// 复杂验证条件
'name' => 'unique:user,status=1&account='.$data['account'],
requireIf:field,value 验证某个字段的值等于某个值的时候必须
// 当uid的值等于1的时候 pwd必须
'pwd'=>'requireIf:uid,1'
requireWith:field 验证某个字段有值的时候必须
// 当user_name有值的时候pwd字段必须
'pwd'=>'requireWith:user_name'
requireCallback:callable #验证当某个callable为真的时候字段必须
// 当username为真时验证pwd字段必须
'pwd'=>'requireCallback:username'
案例
<?php
namespace app\admin\validate;
use think\facade\Db;
use think\Validate;
class BankListVal extends Validate
{
protected $rule = [
'name|银行名称' => ['require','max:20','chs'],
'code|银行logo' => ['require','alpha','checkLogo'=>"Logo文件不存在"], #checkLogo 自定义验证规则
'status.between:0,1' => '认证状态异常!' ,
'msg.requireIf:status,0' => '请输入认证备注!' //当status为0时,msg必填
];
protected $message = [
'name.max' => '银行名称不能超过20个字符!',
'name.chs' => '银行名称输入不是纯汉字!',
'code.alpha' => '银行logo选择有误!'
];
protected $scene = [
'all' => []
];
protected function checkLogo($value, $rule, $data){
//自定义验证逻辑**** start
// ***********
//$checkLogo = ***
//自定义验证逻辑**** end
if($checkLogo) $rule = true;//验证通过返回true
return $rule;
}
}
$validate = new BankListVal();
$checker = $validate->scene('all')->check($editData);
if (!$checker) throw new Exception($validate->getError());