当前位置 : 主页 > 编程语言 > java >

tp6 验证 validate 用法

来源:互联网 收集:自由互联 发布时间:2022-07-22
例子:给后端用户定义一个专属的验证类 User 1.定义规则和提示文字 User.php代码 ?php namespace app\admin\validate; use think\Validate; class User extends Validate { protected $rule = [ 'username' = 'require|chsAlpha


例子:给后端用户定义一个专属的验证类 User

tp6 验证 validate 用法_composer

1.定义规则和提示文字   User.php代码

<?php
namespace app\admin\validate;
use think\Validate;

class User extends Validate
{
protected $rule = [
'username' => 'require|chsAlphaNum|length:6,30',
'password' => 'require|length:6,30',
'verifyCode' => 'require|length:4',
'email' => 'email',
'tel' => 'mobile|number|length:11',
];

protected $message = [
'username.require' => '用户名必须填写',
'username.chsAlphaNum' => '用户名长度范围6-30个字符',
'username.length' => '用户名只能是汉字、字母和数字',
'password.require' => '密码必须填写',
'password.length' => '密码必须6-30个字符',
'verifyCode.require' => '验证码必须填写',
'verifyCode.length' => '验证码必须5位',
'tel.mobile' => '填写有效的手机',
'tel.number' => '手机只能数字',
'tel.length' => '手机只能11位数字',
'email' => '邮箱格式错误',
];

}

2.控制器引用

要引用

use app\admin\validate\User;
use think\exception\ValidateException;

2.1 使用方法一

$postjson=input('post.data/s');//接收所有数据 格式json


try {
if(empty($postjson)){
throw new \Exception("数组为空!");
}
$postArr=json_decode($postjson, true);//把接收所有数据转为数组
//验证表单
validate(ManageValidate::class)->check($postArr);

} catch (ValidateException $e) {
return ['code'=>-200,'msg'=>$e->getError()];
} catch (\Exception $e) {
// 这是进行异常捕获
return ['code'=>-200,'msg'=>$e->getMessage()];
}

2.2  使用方法二

<?php
namespace app\admin\controller;
use think\facade\View;
// use think\db\Where;
use think\facade\Env;
use app\admin\validate\User;
use think\exception\ValidateException;

class Login
{
public function index()
{
return View::fetch();
}


public function login()
{
// $where = new Where;
$sdf=env('CMS_PWD');
// var_dump($sdf);exit;
$username=input('post.username/s');
$password=input('post.password');
$verifyCode=input('post.verifyCode/s');
// $username=input('post.username/s');
try {
//验证
validate(User::class)->check([
'username' => $username,
'password' => $password,
'verifyCode' => $verifyCode,
]);

if (request()->isAjax()){
$rs = Db::name('admin')
->field('username,password,realname,nickname,img,tel,email,department_id,department_id2,role_id,group_id,code_md5,enterprise_id')
->where($where)
->find();
}else{
throw new \Exception("非法提交");
}
$code=200;$msg='成功';
} catch (ValidateException $e) {
return ['code'=>-200,'msg'=>$e->getError()];
} catch (\Exception $e) {
// 这是进行异常捕获
return ['code'=>-200,'msg'=>$e->getMessage()];
}


return json(['code' => $code,'msg' =>$msg]);
}

}

tp6 验证 validate 用法_mysql_02

其它例子:

<?php

namespace app\services\member\validate;

use think\Validate;
use app\services\member\model\UserModel;
use think\facade\Db;

class UserValidate extends Validate
{
use \trait\RequestUtil;
protected $rule = [
'enterprise_id' => 'require|existEid',
'username' => 'require|length:3,20|checkUsername',
'password' => 'require',
'phone' => 'mobile|checkPhone',
'realname' => 'length:2,5',
'nickname' => 'length:3,20',
'email' => 'email',
'avatar' => 'checkImg',
'openid' => 'require|length:10,30',
'qq' => 'number|length:6,20',
'telephone' => 'checkTel',
'idcard' => 'idCard',
'sex' => 'in:0,1',
'sort' => 'number',
'nationality' => 'length:2,10',
'resideprovince' => 'number',
'residecity' => 'number',
'residedist' => 'length:2,10',
];
protected $message=[
'id.require' => 'id获取失败',
'enterprise_id.require' => '所属企业id没有设置',
'username.require' => '用户名没有设置',
'username.length' => '用户名字符长度应大于3小于20',
'phone.mobile' => '电话格式不对',
'realname.length' => '真实姓名字符长度应大于2小于5',
'nickname.length' => '昵称字符长度应大于3小于20',
'email.email' => '邮箱格式不对',
'avatar.checkImg' => '头像路径不对',
'openid.require' => '绑定微信必须openid',
'openid.length' => 'openid格式不对',
'qq.number' => 'QQ号码为纯数字',
'qq.length' => 'QQ号码长度不服',
'telephone.checkTel' => '固话号码格式不对',
'idcard.idCard' => '身份证格式不对',
'sex.in' => '性别选择不对',
'sort.number' => '排序应为数字排序',
];
public $scene=[
'register' => ['enterprise_id','username','password','phone'],
'update' => ['email','avatar','qq','telephone','idcard','sex','realname','nickname','nationality','resideprovince','residecity','residedist'],
];
/**
* @description: 用户名注册检查
* @param {*} $value
* @param {*} $rule
* @param {*} $data
* @return {*}
*/
function checkUsername($value, $rule, $data=[])
{
try{
if(UserModel::connect('mysql')->where('username',$value)->find()){
return '用户名重复';
};
return true;
}catch(\Exception $e){
return $e->getMessage();
}
}
/**
* @description: 电话号码注册检查
* @param {*} $value
* @param {*} $rule
* @param {*} $data
* @return {*}
*/
function checkPhone($value, $rule, $data=[])
{
try{
if(UserModel::connect('mysql')->where('phone',$value)->find()){
return '电话号码已被注册';
};
return true;
}catch(\Exception $e){
return $e->getMessage();
}
}
function checkImg($value, $rule, $data=[])
{
return true;
}
function checkTel($value, $rule, $data=[])
{
return true;
}
/**
* @description: 企业表检查
*/
function existEid($value, $rule, $data=[])
{
if(!Db::connect('enterprise')->query("show tables like 'dc_user_$value'")){
$enterprise = GetEnterpriseCache((int)$value,'config');
if($enterprise && self::checkResult($enterprise)){
if(!Db::connect('enterprise')->query("create table dc_user_$value like dc_user_0")){
return '企业表不存在并自动创建失败';
}
}else{
return '企业id错误或不可用';
}
};
return true;
}

}

官方文档:​​验证器 · ThinkPHP6.0完全开发手册 · 看云​​

上一篇:JAVA从入门到精通竟然这么简单
下一篇:没有了
网友评论