您现在的位置是:首页> 网站开发> ThinkPHP

TP5验证器使用实例

  • 3315人已阅读
  • 时间:2018-10-16 08:58:44
  • 分类:ThinkPHP
  • 作者:祥哥

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;
        }
    }
}


Top