Skip to content

built in validators

Inhere edited this pageFeb 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']