<?php namespace app\index\controller; use think\Controller; use think\Validate; class Index extends Controller { public function index() { $validate = new Validate([ 'name' => 'require|max:10', 'email' => 'email' ]); $data = [ 'name' => '胡勇健', 'email' => '308830232@qq.com' ]; if (!$validate->check($data)) { dump($validate->getError()); } } }
验证器 application/index/validate/User.php
<?php namespace app\index\validate; use think\Validate; class User extends Validate { protected $rule = [ 'name' => 'require|max:10', 'email' => 'email', ]; }
控制器
<?php namespace app\index\controller; use think\Controller; use think\Loader; class Index extends Controller { public function index() { $data = [ 'name' => '胡勇健hhhhhhhhhhhh', 'email' => '308830232@qq.com' ]; $validate = Loader::validate('User'); //或使用助手函数validate //$validate = validate('User'); if (!$validate->check($data)) { dump($validate->getError()); } } }
require
number
float
boolean
array
date
alpha
alphaNum
alphaDash
url
ip
in
between
max:number
// 日期格式验证 Validate::dateFormat('2016-03-09','Y-m-d'); // true // 验证是否有效的日期 Validate::is('2016-06-03','date'); // true // 验证是否有效邮箱地址 Validate::is('thinkphp@qq.com','email'); // true // 验证是否在某个范围 Validate::in('a',['a','b','c']); // true // 验证是否大于某个值 Validate::gt(10,8); // true // 正则验证 Validate::regex(100,'\d+'); // true