TP框架驗證類
阿新 • • 發佈:2018-12-10
具體實現
驗證規則和提示資訊分開定義
$rule = [
'name' => 'require|max:25',
'age' => 'number|between:1,120',
'email' => 'email',
];
$msg = [
'name.require' => '名稱必須',
'name.max' => '名稱最多不能超過25個字元',
'age.number' => '年齡必須是數字',
'age.between' => '年齡必須在1~120之間',
'email' => '郵箱格式錯誤',
];
$data = [
'name' => 'thinkphp',
'age' => 121,
'email' => ' [email protected]',
];
$validate = new Validate($rule,$msg);
$result = $validate->check($data);
if(!$result){
echo $validate->getError();
}
驗證規則與提示資訊一起定義
$rule = [
['name','require|max:25','名稱必須|名稱最多不能超過25個字元'],
['age','number|between:1,120','年齡必須是數字|年齡必須在1~120之間'],
['email','email','郵箱格式錯誤']
];
$data = [
'name' => 'thinkphp',
'age' => 121,
'email' => ' [email protected]',
];
$validate = new Validate($rule);
$result = $validate->check($data);
if(!$result){
echo $validate->getError();
}