一、验证器总结(此处把控制器的验证部分全部放到validate中进行验证)
(一)缺点:适用于一个验证,多个验证时,不易于修改
1.在validate文件创建User文(application/common/validate)
namespace app\common\validate; use think\Validate; class User extends Validate { protected $rule = [ 'name' => 'require|max:25', 'age' => 'number|between:1,120', 'email' => 'email', 'password' => 'require|min:6|max:12' ]; protected $message = [ 'name.require' => '名称必须', 'name.max' => '名称最多不能超过25个字符', 'age.number' => '年龄必须是数字', 'age.between' => '年龄只能在1-120之间', 'email' => '邮箱格式错误', 'password.require' => '请输入密码', 'password.min' => '密码需介于6-12位', 'password.max' => '密码需介于6-12位', ]; }
2.在controller文件创建控制文件
namespace app\index\controller; use app\common\validate; use think\Controller; class User extends Controller { public function register(){ $data = [ 'name' => 'thinkphp', 'age' => 10, 'email' => 'thinkphp@qq.com', 'password' => '123456', ]; #验证数据 $validate = new \app\common\validate\User(); //$result = $this->validate($data ,'User'); $result = $validate->check($data); #数据验证错误 if(!$result){ echo $validate->getError(); } } }
(二)验证场景,优点:可用于多个验证;缺点:字段多的时候,不易于修改
1.validate文件下建User
namespace app\common\validate; use think\Validate; class User extends Validate { protected $rule = [ 'name' => 'require|max:25', 'age' => 'number|between:1,120', 'email' => 'email', ]; protected $message = [ 'name.require' => '名称必须', 'name.max' => '名称最多不能超过25个字符', 'age.number' => '年龄必须是数字', 'age.between' => '年龄只能在1-120之间', 'email' => '邮箱格式错误', ]; protected $scene = [ 'add' => ['name','age'], 'edit' => ['name'], ]; }
2.controller文件下建User
namespace app\index\controller; use think\Controller; class User extends Controller { public function register(){ $data = [ 'name' => 'thinkphp', 'age' => 10, 'email' => 'thinkphp@qq.com', ]; #验证数据 $validate = new \app\common\validate\User(); $result = $validate->scene('add')->check($data);#与(一)不同之处 #数据验证错误 if(!$result){ echo $validate->getError(); } #或者,场景验证 // $result = $this->validate($data,'User.add'); #不使用验证场景 // $result = $this->validate($data,'User'); // if(true !== $result){ dump($result); // } } public function edit(){ $data = [ 'name' => 'thinkphp', 'age' => 10, 'email' => 'thinkphp@qq.com', ]; #验证数据 $validate = new \app\common\validate\User(); $result = $validate->scene('edit')->check($data);#与(一)不同之处 #数据验证错误 if(!$result){ echo $validate->getError(); } } }
(三)验证器和验证内容分开 优点:可用于多个验证,多字段易于修改,缺点:代码多了些
1.validate文件
1.1借助tp的构造函数,定义好验证方法,新建ValidateFun
namespace app\common\validate; use think\Validate; class ValidateFun extends Validate{ public function __construct(array $rules = [], array $message = [], array $field = []) { parent::__construct($rules, $message, $field); } }
1.2新建Validate
namespace app\common\validate; class Validate { #const 定义一个变量,与define的功能类似,也有区别,详情自己百度 const ErrMsg = [ 'title.require' =>'角色名称不能为空', 'title.checkRoleTitleIs' =>'角色名称已存在', 'name.require' =>'名称不能为空', ]; public static function add(){ $rule = [ 'title' => 'require|checkRoleTitleIs' ]; $message = self::ErrMsg; return ['rule'=>$rule,'message'=>$message]; } public static function edit(){ $rule = [ 'title' => 'require', 'name' => 'require', ]; $message = self::ErrMsg; return ['rule'=>$rule,'message'=>$message]; } }
或者(上面的写法的优点是:相同字段的验证,写一次就好)
namespace app\index\validate; class Validate { public static function add(){ $rule = [ 'title' => 'require|checkRoleTitleIs' ]; $message = [ 'title.require' =>'角色名称不能为空', 'title.checkRoleTitleIs' =>'角色名称已存在', ]; return ['rule'=>$rule,'message'=>$message]; } }
2.controller
namespace app\common\controller; use app\index\validate; use think\Controller; class User extends Controller { public function register(){ $data = [ 'name' => 'thinkphp', 'age' => 10, 'email' => 'thinkphp@qq.com', ]; #验证数据 $vali = Validate::add();#实例化方法 $validate = new ValidateFun($vali['rule'], $vali['message']); if (!$validate->check($check_data)) { echo $validate->getError(); } } }
注意:
1.自己新建验证规则
自定义验证规则的方法,此方法放到继承validate的文件中,例如如下代码,checkRoleTitleIs为自定义规则
namespace app\index\validate; class Validate { public static function add(){ $rule = [ 'title' => 'require|checkRoleTitleIs' ]; $message = [ 'title.require' =>'角色名称不能为空', 'title.checkRoleTitleIs' =>'角色名称已存在', ]; return ['rule'=>$rule,'message'=>$message]; } public function checkRoleTitleIs($v){ $info = Db::name('role')->where(['name'=>$v])->find(); if($info){#存在 return false; }else{ return true; } }
2.模型验证(一条代码,包含验证和插入数据库的功能,验证成功即将该数据插入数据库)
没有用验证器的模型验证:
namespace app\index\controller; use think\Controller; class User extend Controller{ $User = new app\index\model\User;#实例化模型 $result = $User->validate(#验证信息 [ 'name' => 'require|max:25', 'email' => 'email', ], [ 'name.require' => '名称必须', 'name.max' => '名称最多不能超过25个字符', 'email' => '邮箱格式错误', ] )->save($data);#保存数据 if(false === $result){ // 验证失败 输出错误信息 dump($User->getError()); } }
有验证器的模型验证:
namespace app\common\validate; use think\Validate; class User extends Validate { protected $rule = [ 'name' => 'require|max:25', 'email' => 'email', ]; protected $message = [ 'name.require' => '用户名必须', 'email' => '邮箱格式错误', ]; protected $scene = [ 'add' => ['name','email'], 'edit' => ['email'], ]; }
namespace app\index\controller; use think\Controller; class User extends Controller{ $User = new app\index\model\User;#实例化模型 $result = $User->validate(true)->save($data);#保存数据(此处,该数据表的名称为user) // $result = $User->validate(‘member’)->save($data);#保存数据(此处,该数据表的名称为member) // $result = $User->validate('User.edit')->save($data);#(此处,有场景验证) if(false === $result){ // 验证失败 输出错误信息 dump($User->getError()); } }
最新回复 (0)