TP5验证器建立模块\validate\验证器文件名

例如我们定义了一个验证USER的验证器类<?php

namespace app\admin\validate;

use think\Validate;

class Admin extends Validate{

protected $rule = [

'username' => 'require|min:3|max:16|unique:admin',

'password' => 'require|min:6|max:32',

];

protected $message = [

'username.require' => '管理员名称不能为空',

'password.require' => '管理员密码不能为空',

'username.min' => '管理员名称最少应为3位',

'username.unique' => '管理员名称已经存在',

'password.min' => '管理员密码最少应为6位',

'username.max' => '管理员名称不能超过16位',

'password.max' => '管理员密码不能超过32位',

];

protected $scene=[

'edit' => ['username'],

'add' => ['username','password'],

];

}

验证器的使用在需要验证的地方可以使用//验证数据

$validate = Loader::validate('Admin');

if(!$validate->scene('edit')->check($data)){

$this->error($validate->getError());die;

}

扩展正则在验证器类里的使用方法<?php

namespace app\index\validate;

use think\Validate;

class User extends Validate{

// 正则自定义验证手机方法

protected $regex = [ 'mobile' => '/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/'];

protected $rule = [

'username' => 'require|min:6|max:16|unique:user',

'password' => 'require|min:6',

//数组的写法'mobile' => 'require|regex:mobile',

'mobile' => ['require','regex'=>'mobile'],

'mail' => 'require|email',

'captcha' => 'require|captcha',

];

protected $message = [

'username.require' => '用户名不能为空',

'username.min' => '用户名至少为6个字符',

'username.max' => '用户名最多为16个字符',

'username.unique' => '用户名已存在',

'password.require' => '密码不能为空',

'password.min' => '密码至少为6个字符',

'mobile.require' => '手机号不能为空',

'mobile.regex' => '手机号填写不正确',

'mial.require' => 'E-mail不能为空',

'mial.email' => 'E-mail格式不正确',

'captcha.require' => '验证码不能为空',

'captcha.captcha' => '验证码不正确',

];

protected $scene=[

'add' => ['username','password','mobile','mail','captcha'],

];

}// 验证规则

protected $rule = [

'goods_id' => 'checkGoodsId',

'goods_name' => 'require|min:3|max:150|unique:goods',

'cat_id' => 'number|gt:0',

'goods_sn' => 'unique:goods|max:20',

'shop_price' => ['require', 'regex' => '([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9])'],

'market_price' => 'require|regex:\d{1,10}(\.\d{1,2})?$|checkMarketPrice',

'weight' => 'regex:\d{1,10}(\.\d{1,2})?$',

'give_integral' => 'regex:^\d+$',

'is_virtual' => 'checkVirtualIndate',

'exchange_integral' => 'checkExchangeIntegral',

'is_free_shipping' => 'require|checkShipping',

'commission' => 'checkCommission'

];

//错误信息

protected $message = [

'goods_name.require' => '商品名称必填',

'goods_name.min' => '名称长度至少3个字符',

'goods_name.max' => '名称长度至多50个汉字',

'goods_name.unique' => '商品名称重复',

'cat_id.number' => '商品分类必须填写',

'cat_id.gt' => '商品分类必须选择',

'goods_sn.unique' => '商品货号重复',

'goods_sn.max' => '商品货号超过长度限制',

'goods_num.checkGoodsNum' => '抢购数量不能大于库存数量',

'shop_price.require' => '本店售价必填',

'shop_price.regex' => '本店售价格式不对',

'market_price.require' => '市场价格必填',

'market_price.regex' => '市场价格式不对',

'market_price.checkMarketPrice' => '市场价不得小于本店价',

'weight.regex' => '重量格式不对',

'give_integral.regex' => '赠送积分必须是正整数',

'exchange_integral.checkExchangeIntegral' => '积分抵扣金额不能超过商品总额',

'is_virtual.checkVirtualIndate' => '虚拟商品有效期不得小于当前时间',

'is_free_shipping.require' => '请选择商品是否包邮',

];

继续扩展自定义验证器类namespace app\index\validate;

use think\Validate;

class User extends Validate

{

protected $rule = [

'name'  =>  'checkName:thinkphp',

'email' =>  'email',

];

protected $message = [

'name'  =>  '用户名必须',

'email' =>  '邮箱格式错误',

];

// 自定义验证规则

protected function checkName($value,$rule,$data)

{        return $rule == $value ? true : '名称错误';

}

}

自定义验证方法可以传入5个参数,后面的三个可以选用顺序为

验证数据,验证规则,全部数据,字段名,字段描述use think\Validate;

class User extends Validate{

protected $rule = [

'username' => 'require|min:6|max:16|unique:user',

'password' => 'require|min:6',

'mobile' => ['require','checkMobile'],

'mail' => 'require|email',

'captcha'=>'require|captcha',

];

protected $message = [

'username.require' => '用户名不能为空',

'username.min' => '用户名至少为6个字符',

'username.max' => '用户名最多为16个字符',

'username.unique' => '用户名已存在',

'password.require' => '密码不能为空',

'password.min' => '密码至少为6个字符',

'mobile.require' => '手机号不能为空',

'mail.require' => 'E-mail不能为空',

'mail.email' => 'E-mail格式不正确',

'captcha.ruquire' => '验证码不能为空',

'captcha.captcha' => '验证码不正确',

];

protected $scene=[

'add' => ['username','password','mobile','mail','captcha',],

];

//自定义验证手机号格式

protected function checkMobile($value)

{

//检测手机号码是否合法

if(!preg_match("/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/",$value)){

return '手机号格式不正确';

}else{

return true;

}

}

}

tp5 php正则邮箱,TP5验证器使用实例相关推荐

  1. Spring MVC验证器应用实例(超详细)

    本节使用一个应用 springMVCDemo08 讲解 Spring 验证器的编写及使用.该应用中有一个数据输入页面 addGoods.jsp,效果如图 1 所示. 有一个数据显示页面 goodsLi ...

  2. Spring MVC验证器:Validator接口和ValidationUtils类

    本节主要介绍创建自定义 Spring 验证器时需要实现的 Validator 接口和工具类 ValidationUtils. Validator接口 创建自定义 Spring 验证器需要实现 org. ...

  3. TP5:验证器的封装——5

    TP5的独立验证器如图所示: $validate = new Validate(['name' => 'require|max:25','email' => 'email' ]); $da ...

  4. javascript正则检测用户名验证密码邮箱手机号

    正则验证用户名密码手机号邮箱 html <body><!--账号--><form><div> 请输入账号:<input type="te ...

  5. 常用正则验证 :手机号、验证码、密码、邮箱等验证

    常用正则验证 :手机号.验证码.密码.邮箱等验证 参考地址:https://www.runoob.com/regexp/regexp-tutorial.html https://blog.csdn.n ...

  6. php正则邮箱验证,两种PHP用户注册邮箱验证正则表达式方法

    一般我们在WEB端新注册或者登陆账户的时候,对于字段的验证不仅仅是需要验证其是否为空或者长度,有些特殊的字段,比如EMAIL邮箱我们需要验证是不是邮箱的格式,比如是都带有@符号等.刚才在练习PHP程序 ...

  7. php验证器的调用,ThinkPHP5 验证器的具体使用

    这篇文章主要介绍了关于ThinkPHP5 验证器的具体使用,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下 前言: 我们在做API开发的时候,我们会接受客户端传来的参数,大家都知道这个参 ...

  8. php validator,实用的PHP验证器类Validator

    使用composer安装 提供非常方便的composer安装: composer require particle/validator 使用 在使用之前请确保在项目中引入了 vendor/autolo ...

  9. yii 验证器类 细说YII验证器

    在 yii-1.1.10.r3566 版本中,yii自带的验证器共有 19 个.全部如下: // CValidator.php public static $builtInValidators=arr ...

最新文章

  1. 三代测序数据分析之文献推荐
  2. 技术 | Web前端开发(4)持续更新
  3. kubeadm部署k8s_(Ansible)三分钟部署一套高可用/可扩展的kubeadm集群
  4. AOP 中必须明白的概念-目标对象(Target Object)
  5. sgu 175 Encoding
  6. 分解质因数-洛谷P3200 [HNOI2009]有趣的数列
  7. 前端学习(2915):数据绑定
  8. 利用C语言中的setjmp和longjmp,来实现异常捕获和协程
  9. asp.net数据库连接web.config配置
  10. 开发WinRT自定义组件之富文本框
  11. 现控笔记(三):状态空间表达式的解
  12. 我就是这样顺利拿到腾讯和微软的offer,
  13. 记录数据库面试题及答案21~41
  14. 拯救者 linux 无线网卡驱动下载,联想y7000无线网卡驱动下载-联想拯救者y7000无线网卡驱动v19.51.22.2 官方版 - 极光下载站...
  15. c语言岩石1ms,2017年注册岩土工程师基础考试真题下午和答案解析
  16. 长方形的周长公式的c语言,长方形周长公式
  17. mysql 分库备份_如何分表分库备份及批量恢复?MySQL
  18. win10环境socks代理实战
  19. 0102Linux基础命令
  20. 无锡学python_无锡python基础编程好学吗

热门文章

  1. 3、jeecg 笔记之 模糊查询
  2. lwip连续发数据卡死_Mysteel:12月全球铁矿石发运量稳中微增 进口矿咋走?
  3. JDK8后的日期时间API
  4. @EqualsAndHashCode()注解详解
  5. java 实现超时_如何实现带有超时的Runnable? - java
  6. .net 日期总结,用于业务时间查询
  7. docker 使用中遇到的问题
  8. python之Map函数 reduce 函数
  9. 用小程序·云开发打造功能全面的博客小程序丨实战
  10. 【论文阅读】Deep Adversarial Subspace Clustering