Correction status:Uncorrected
Teacher's comments:
thinkphp 验证器
application/index/controller/Verify.php
<?php namespace app\index\controller; use think\Controller; use app\index\validate\Staff; use think\Validate; class Verify extends Controller { public function demo1() { $data=[ 'name'=>'er345', 'sex'=>0, 'age'=>56, 'salary'=>19000 ]; $validate=new Staff(); if (!$validate->check($data)){ dump($validate->getError()); } else { return '验证通过'; } } }
点击 "运行实例" 按钮查看在线实例
application/index/validate/Staff.php
<?php namespace app\index\validate; use think\Validate; class Staff extends Validate { protected $rule=[ 'name'=>'require|min:4|max:10', 'sex'=>'in:0,1', 'age'=>'require|between:18,60', 'salary'=>'require|gt:2000' ]; // wrong msg protected $message=[ 'name.require'=>'员工姓名不为空', 'name.min'=>'姓名长度不小于4', 'name.max'=>'姓名长度不大于10', 'sex.in'=>'性别只可以选择男女', 'age.require'=>'年龄为必填', 'age.between'=>'年龄在18岁到60岁', 'salary.require'=>'收入为必填', 'salary.gt'=>'收入需要大于2000' ]; }
点击 "运行实例" 按钮查看在线实例
url 访问 http://tp.top/index.php/index/verify/demo1
全部验证通过显示 验证通过,没有验证通过根据 application/index/validate/Staff.php,$message 提示错误信息