具体请查看thinkphp5开发手册
新建验证器 文件
<?php
namespace app\admin\validate;use think\Validate;/*** 商户平台 验证器* ==============================================* 未经授权不许任何使用和传播。* ==============================================* @date: 2021/12/16 14:40* @author: * @version: 1.0*/
class Vmerchant extends Validate
{
protected $rule = ['name' => 'require|max:100|number','email' => 'email',];protected $message = ['name.require' => '基础信息-名称必填','name.max' => '名称最多不能超过100个字符','name.number' => '必须是数字',];}
<?php
namespace app\admin\logic;
use app\admin\validate\Vmerchant;/*** 商户平台 逻辑处理=》1连锁商户 2平台商户 3保险商户* ==============================================* 未经授权不许任何使用和传播。* ==============================================* @date: 2021/12/13 17:30* @author: * @version: 1.0*/
class MerchantLogic
{
protected $successCode;protected $errorCode;public function __construct($postsata){
$this->postdata = $postsata;$this->Vmerchant = new Vmerchant(); //引入}/*** 商户编辑* @param $params* @return array* @throws \think\Exception* @throws \think\exception\PDOException*/public function merchantEdit($params){
$validateData['name'] = $this->postdata['name'];if (!$this->Vmerchant->check($validateData)) {
dump($this->Vmerchant->getError()); //打印错误报错信息}}}//自定义规则验证 错误提示可以全部写在Vdrugs message里面
$rule3 = ['third_class_pid' => 'require','third_class_name' => 'require',
];
if (!$this->Vmerchant->check($this->postdata, $rule3)) {
return ['code' => Errorcode::PARAMSERR, 'message' => $this->Vmerchant->getError(), 'data' => []];
}//批量验证
$rule3 = ['third_class_pid' => 'require','third_class_name' => 'require',
];
if (!$this->Vmerchant->make($rule3)->batch()->check($this->postdata)) {
return ['code' => Errorcode::PARAMSERR, 'message' => $this->Vmerchant->getError(), 'data' => []];
}
封装使用
use app\mall\validate\Vmall;//自己定义的验证器 或者传入self::Validate($post,new Vindex(),['type' => 'require',]);protected static function mallValidate($data,$v,$rule = [],$message = []){
//TODO 验证信息$Vmall = new Vmall();if (!$Vmall->check($data,$rule)) {
// $Vmall->check($data,$rule)
// $Vmall->make($rule,$message)->batch()->check($data)exit(json_encode(['code'=>1000,'msg'=>$v->getError(),'data'=>'']));}}