Skip to content

built in validators

Inhere edited this page Feb 12, 2019 · 1 revision

/ 分隔的验证器,表明功能是一样的,只是有不同的别名

验证器 说明 规则示例
required 要求此字段/属性是必须的(不为空的)。关于为空 ['tagId, userId', 'required' ]
int/integer 验证是否是 int 支持范围检查 ['userId', 'int'] ['userId', 'int', 'min'=>4, 'max'=>16]
num/number 验证是否是 number(大于0的整数) 支持范围检查 ['userId', 'number'] ['userId', 'number', 'min'=>4, 'max'=>16]
bool/boolean 验证是否是 bool. 关于bool值 ['open', 'bool']
float 验证是否是 float ['price', 'float']
string 验证是否是 string. 支持长度检查 ['name', 'string'], ['name', 'string', 'min'=>4, 'max'=>16]
accepted 验证的字段必须为 yes/on/1/true 这在确认「服务条款」是否同意时有用(ref laravel) ['agree', 'accepted']
url 验证是否是 url ['myUrl', 'url']
email 验证是否是 email ['userEmail', 'email']
alpha 验证值是否仅包含字母字符 ['name', 'alpha']
alphaNum 验证是否仅包含字母、数字 ['field', 'alphaNum']
alphaDash 验证是否仅包含字母、数字、破折号( - )以及下划线( _ ) ['field', 'alphaDash']
map/isMap 验证值是否是一个非自然数组 map (key - value 形式的) ['goods', 'isMap']
list/isList 验证值是否是一个自然数组 list (key是从0自然增长的) ['tags', 'isList']
array/isArray 验证是否是数组 ['goods', 'isArray']
each 对数组中的每个值都应用给定的验证器(这里的绝大多数验证器都可以使用),并且要全部通过 ['goods.*','each','string'], ['goods.*','each','string','min'=>3]
hasKey 验证数组存在给定的key(s) ['goods', 'hasKey', 'pear'] ['goods', 'hasKey', ['pear', 'banana']]
distinct 数组中的值必须是唯一的 ['goods', 'distinct'], ['users.*.id', 'distinct']
ints/intList 验证字段值是否是一个 int list ['tagIds', 'intList']
numList 验证字段值是否是一个 number list ['tagIds', 'numList']
strings/strList 验证字段值是否是一个 string list ['tags', 'strList']
arrList 验证字段值是否是一个 array list(多维数组) ['tags', 'arrList']
min 最小边界值验证 ['title', 'min', 40]
max 最大边界值验证 ['title', 'max', 40]
size/range/between 验证大小范围, 可以支持验证 int, string, array 数据类型 ['tagId', 'size', 'min'=>4, 'max'=>567]
length 长度验证( 跟 size差不多, 但只能验证 string, array 的长度 ['username', 'length', 'min' => 5, 'max' => 20]
fixedSize/sizeEq/lengthEq 固定的长度/大小(验证 string, array 长度, int 大小) ['field', 'fixedSize', 12]
startWith 值(string/array)是以给定的字符串开始 ['field', 'startWith', 'hell']
endWith 值(string/array)是以给定的字符串结尾 ['field', 'endWith', 'world']
in/enum 枚举验证: 包含 ['status', 'in', [1,2,3]]
notIn 枚举验证: 不包含 ['status', 'notIn', [4,5,6]]
inField 枚举验证: 字段值 存在于 另一个字段(anotherField)的值中 ['field', 'inField', 'anotherField']
eq/mustBe 必须是等于给定值 ['status', 'mustBe', 1]
ne/neq/notBe 不能等于给定值 ['status', 'notBe', 0]
eqField 字段值比较: 相同 ['passwd', 'eqField', 'repasswd']
neqField 字段值比较: 不能相同 ['userId', 'neqField', 'targetId']
ltField 字段值比较: 小于 ['field1', 'ltField', 'field2']
lteField 字段值比较: 小于等于 ['field1', 'lteField', 'field2']
gtField 字段值比较: 大于 ['field1', 'gtField', 'field2']
gteField 字段值比较: 大于等于 ['field1', 'gteField', 'field2']
requiredIf 指定的其它字段( anotherField )值等于任何一个 value 时,此字段为 必填(ref laravel) ['city', 'requiredIf', 'myCity', ['chengdu'] ]
requiredUnless 指定的其它字段( anotherField )值等于任何一个 value 时,此字段为 不必填(ref laravel) ['city', 'requiredUnless', 'myCity', ['chengdu'] ]
requiredWith 指定的字段中的 任意一个 有值且不为空,则此字段为 必填(ref laravel) ['city', 'requiredWith', ['myCity'] ]
requiredWithAll 如果指定的 所有字段 都有值,则此字段为 必填(ref laravel) ['city', 'requiredWithAll', ['myCity', 'myCity1'] ]
requiredWithout 如果缺少 任意一个 指定的字段值,则此字段为 必填(ref laravel) ['city', 'requiredWithout', ['myCity', 'myCity1'] ]
requiredWithoutAll 如果所有指定的字段 都没有值,则此字段为 必填(ref laravel) ['city', 'requiredWithoutAll', ['myCity', 'myCity1'] ]
date 验证是否是 date ['publishedAt', 'date']
dateFormat 验证是否是 date, 并且是指定的格式 ['publishedAt', 'dateFormat', 'Y-m-d']
dateEquals 验证是否是 date, 并且是否是等于给定日期 ['publishedAt', 'dateEquals', '2017-05-12']
beforeDate 验证字段值必须是给定日期之前的值(ref laravel) ['publishedAt', 'beforeDate', '2017-05-12']
beforeOrEqualDate 字段值必须是小于或等于给定日期的值(ref laravel) ['publishedAt', 'beforeOrEqualDate', '2017-05-12']
afterOrEqualDate 字段值必须是大于或等于给定日期的值(ref laravel) ['publishedAt', 'afterOrEqualDate', '2017-05-12']
afterDate 验证字段值必须是给定日期之前的值 ['publishedAt', 'afterDate', '2017-05-12']
json 验证是否是json字符串(默认严格验证,必须以{ [ 开始) ['goods', 'json'] ['somedata', 'json', false] - 非严格,普通字符串eg 'test'也会通过
file 验证是否是上传的文件 ['upFile', 'file']
image 验证是否是上传的图片文件 ['avatar', 'image'], 限定后缀名 ['avatar', 'image', 'jpg,png']
ip 验证是否是 IP ['ipAddr', 'ip']
ipv4 验证是否是 IPv4 ['ipAddr', 'ipv4']
ipv6 验证是否是 IPv6 ['ipAddr', 'ipv6']
macAddress 验证是否是 mac Address ['field', 'macAddress']
md5 验证是否是 md5 格式的字符串 ['passwd', 'md5']
sha1 验证是否是 sha1 格式的字符串 ['passwd', 'sha1']
color 验证是否是html color ['backgroundColor', 'color']
regex/regexp 使用正则进行验证 ['name', 'regexp', '/^\w+$/']
safe 用于标记字段是安全的,无需验证 ['createdAt, updatedAt', 'safe']