Correction status:Uncorrected
Teacher's comments:
controller: <?php namespace app\index\controller; use think\Controller; use app\validate\Staff; use think\Validate; class Verify extends Controller { public function test() { $data = [ 'name'=>'guanyu', 'sex' => 1, 'age' => 50, 'salary' => 3400 ]; $validate = new Staff(); if (!$validate->check($data)) { dump($validate->getError()); } else { return '验证成功'; } } } validate: <?php namespace app\validate; use think\Validate; class Staff extends Validate { protected $rule = [ 'name'=>'require|min:3|max:10', 'sex' => 'require|in:0,1', 'age' => 'require|between:20,50', 'salary' => 'require|gt: 3000' ]; protected $message = [ 'name.require' => '姓名不能为空', 'name.min' => '姓名长度不能少于3个字符', 'name.max' => '姓名长度不能大于10个字符', 'sex.require' => '性别不能为空', 'sex.in' => '性别只能选择男或女', 'age.require' => '年龄不能为空', 'age.between' => '年龄必须在20到50之间', 'salary.require' => '工资必须输入', 'salary.gt' => '工资必须大于3000元' ]; }