PHP code example of youloge / webman.validate
1. Go to this page and download the library: Download youloge/webman.validate library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
youloge / webman.validate example snippets
$params = [
'username' => 'admin',
'password' => '123456',
];
$rules =[
'username' => ' if(count($value) < 8){
throw new Exception("密码长度不足~");
}
// 验证通过 并处理数据
return md5($value);
}
];
@['err'=>$err,'msg'=>$msg] = $data = useValidate($params,$rules,$filter=true);
if($err === 400) return json(['err'=>400,'msg'=>$msg]);
$rules =[
// length:64,6,30 (不是书写错误 不区分变量数量 不区分变量前后)
'title' => '能是1,2,3,不能是其他值
'status' => '�化保留2位小数 最大100
'created' => '效最后一个哦`]
// 'tags' =>',所以下面的规则会循环验证数组内的对象
'list' => [
[
'name'=>'必填,需要为ip格式
]
];
$params = $request->all();
@['err'=>$err,'msg'=>$msg] = $data = useValidate($params,$rules,$filter=true);
if($err === 400){
return json(['err'=>400,'msg'=>$msg]);
}
$rule_one = ['label'=>'['length:2,20']]; // 循环验证 label字段 每个字段都验证 length:2,20
// 注意:数组规则(多个 大于1)时候: 字符串规则 数组符号[]包裹规则
// 如果数组规则只有一个 那个它表示 对数组进行 循环验证 这是 V2 版本的新特性
@['err'=>$err,'msg'=>$msg] = $data_one = useValidate($params,[
'label'=>[
' ]
]);
if($err === 400){ return json(['err'=>400,'msg'=>$msg]); }
// 验证通过输出 是一个逗号连起来的长字符串