-
-
Notifications
You must be signed in to change notification settings - Fork 127
验证器
vic edited this page Sep 18, 2020
·
3 revisions
$_POST = [
'email' => 'xxxx',
'age' => 2
];
$vt = new Validator();
$result = $vt->setAliases([
'name' => '用户名',
'email' => '邮箱',
'age' => '年龄'
])->validate($_POST, [
'name' => 'required|min_len:5|max_len:10', // 必填 5<= strlen(name) <=10
'email' => 'required|email', // 必填 email 格式
'age' => 'uint|min:18|max:200' // 选填 正整数 18<= age <=200
])->isOk();
if ($result === false) {
print_r($vt->getErrs());
}
//Array
//(
// [0] => 用户名不能为空
// [1] => 邮箱格式不正确
// [2] => 年龄不能小于18
//)
- required 必填
- numeric 数字包括浮点数
- min 不能小于
- max 不能大于
- min_len 不能短于
- max_len 不能长于
- int 整数
- uint 正整数 // 1.9.3以前叫 unsigned_int
- email 邮箱格式
- ip ip格式
- stop 阻止冒泡(当有一条规则不符合时,不验证剩下的规则)
$vt->addRule('between', [
'msg' => ':attribute只能在:arg1-:arg2之间',
'fn' => function ($value, $arg1, $arg2) {
return $value >= $arg1 && $value <= $arg2;
}
]);
$vt->validate(['a' => 10], [
'a' => 'required|between:3,10' //必填 只能在3-10
]);