abstract: 'require|max:25', 'sex'=>'in:0,1',
namespace app\validate;
use think\Validate;
class User extends Validate
{
protected $rule =[
'name'=> 'require|max:25',
'sex'=>'in:0,1',
'age'=>'integer|between:18,60',
// 'age'=>'number|betwenn:18,60',
'email'=>'require|email',
];
protected $message =[
'name.require'=>'名称不能为空',
'name.max'=>'名称最多不能超过25个字符',
'sex'=>'性别只能为0或者1',
'age.numbre'=>'年龄必须是数字',
'age.between'=>'年龄只能在18-60之间',
'email.require'=>'邮箱格式不能为空',
'email.email'=>'邮箱格式错误',
];
}
namespace app\index\controller;
use think\Controller;
use app\validate\User as Sunte;
/**
*
*/
class Userl extends Controller
{
public function add()
{
$data =[
'name'=>'杨俊杰',
'sex'=>1,
'age'=>18,
'email'=>'209531980@qq.com',
];
$Sunte = new Sunte;
if (!$Sunte->check($data)) {
dump($Sunte->getError());
}else{
return "验证通过";
}
}
}
Correcting teacher:天蓬老师Correction time:2019-05-10 15:53:15
Teacher's summary:任何时候 , 都可以用独立验证,控制器内置的那个快捷方法也不错的