在thinkphp5.1 自定义验证规则 获取get 传递的值的时候 传递了 值 能够获取到 验证出错

控制器:

    public function teamDetail(){
       if(request()->isGet()){
           $team_id = $this->request->get('team_id', '');
           $this->validate->scene('teamDetail')->check($team_id);
           if ($this->validate->getError()) {
               return resultArray(lang(strval($this->validate->getError())));
           }
           $page = $this->request->param('page', '1');
           $limit = $this->request->param('limit', '10');
           return $this->logic->teamDetail($team_id,$page,$limit);
       }
       return '';
    }

验证规则:

	['规则1','规则2'...]
     *
     * @var array
     */	
	protected $rule = [
        'goodsName' => 'require|max:255', // 商品名称,必需且最大长度为255个字符
        'goodsImage' => 'require|url', // 商品图片,必需且为有效的URL
        'tags' => 'max:255', // 标签,最大长度为255个字符
        'price' => 'require|float|gt:0', // 价格,必需且为浮点数
        'contact' => 'require|mobile', //  买家联系方式,必需且为手机号码
        'property' => 'require|json', // ,必需且为json
        'dgId' => 'require|integer' , //商品id
        'addressId' =>'require|string', //用户的地址
        'orderId' => 'require|integer', //订单id
        'orderType' => 'require|string' , //订单类型 奖购 团购  闪购
        'maxBuyer' => 'require|integer|gt:0',   //团购最大参团人数
        'winner'   => 'require|integer|gt:0' ,   //团购中将人数
        'introduction' => 'require' ,  //商品简介
        'phone' => 'require|mobile',  //卖家电话
        'consignee' => 'require', //收货人
        'user_tel' => 'require|mobile', //收货人电话
        'status' => 'require|integer' ,// 上下架状态
        'fpeId' => 'require|integer', // 域id
        'goodId' => 'require|string' , //卖出-团购-收货信息
        'publish_area' =>'require',  //发布域
        'team_id' => 'require'      //主订单id

    ];
    
    /**
     * 定义错误信息
     * 格式:'字段名.规则名'	=>	'错误信息'
     *
     * @var array
     */	
    protected $message = [
        'goodsName.max' => '商品名称最大长度不能超过255个字符!',
        'goodsName.require' => '商品名称不能为空!', // 商品名称,必需且最大长度为255个字符
        'goodsImage.require' => '商品图片不能为空!', // 商品图片,必需且为有效的URL
        'goodsImage.url' => '请上传图片',  //
        'tags.max' => ' 标签最大长度为255个字符!', // 标签,最大长度为255个字符
        'price.require' => '请输入有效的价格!', // 价格,必需且为浮点数
        'price.float' => '请输入有效的价格!', // 价格,必需且为浮点数
        'price.gt' => '请输入有效的价格!', // 价格,必需且为浮点数
        'contact.require' => '手机号码格式不正确!', // 联系方式,必需且为手机号码
        'contact.mobile' => '手机号码格式不正确!', // 联系方式,必需且为手机号码
        'property.require' => '请设置奖励参数', // 属性,必需且为布尔值(true或false)
        'property.json' => '请设置奖励参数为josn格式', // 属性,必需且为布尔值(true或false)
        'dgId.require' => "商品id不能为空",
        'dgId.integer' => '商品Id必须为int类型',
        'addressId.require' => '收货地址不能为空',
        'addressId.string' => '收货地址错误',
        'orderId.require'  => '订单id不能为空',
        'orderId.integer' => '订单id不符合要求',
        'orderType.require' => '订单类型不能为空',
        'orderType.string' => '订单类型错误',
        'maxBuyer.require' => '团购人数不能为空',
        'maxBuyer.integer' => '团购人数必须为整数',
        'maxBuyer.gt' => '团购人数必须为大于0',
        'winner.require' =>'获奖人数不能为空',
        'winner.integer' =>'获奖人数必须为整数',
        'winner.gt' =>'获奖人数必须大于0',
        'introduction.require' => '商品简介不能为空',
        'phone.require' => '卖家电话电话不能为空',
        'phone.mobile' => '卖家电话格式不正确',
        'consignee.require' => '收货人不能为空',
        'user_tel.require' => '收货人电话不能为空',
        'user_tel.mobile' => '收货人电话格式不正确',
        'status.require' => '商品的上下架状态不能为空',
        'status.integer' => '商品上下架状态不符合要求',
        'goodId.require' => '商品id不能为空',
        'goodId.string' => '商品id不正确',
        'publish_area.require' => '发布区域不能为空',
        'team_id.require' => '主订单Id不能为空'
    ];

    protected $scene = [
        'order' => ['goodsName','price','goodsImage','tags','contact','property','publish_area'],//奖购
        'userOrder' => ['dgId','addressId','property'],
        'finishedOrder' => ['orderId'],
        'grounding' => ['dgId'],
        'goodsList' => ['goodsName'],
        'release' => ['goodsName','goodsImage','orderType','price','tags','contact','introduction','phone'],//发布
        'qiangShopping' => ['goodsName','price','goodsImage','tags','contact','publish_area'],    //闪购
        'teamShopping' => ['goodsName','goodsImage','tags','price','contact','maxBuyer','winner','publish_area'],  //团购
        'placeOrder' =>['dgId','addressId','consignee','user_tel'], //下单
        'goodSwitch' => ['dgId','status'], //上下架
        'modifyRewardGood' => ['dgId','goodsName','goodsImage','status','price','introduction','phone','tags','property'], //编辑奖购
        'modifyTeamGood' =>['dgId','goodsName','goodsImage','status','price','introduction','phone','tags','maxBuyer','winner'],//编辑团购
        'modifyQiangGood' => ['dgId','goodsName','goodsImage','status','price','introduction','phone','tags'],//编辑闪购
        'goodsDetail' => ['dgId'] , //订单详情
        'teamList' => ['dgId'], //卖出-团购-团购详情
        'teamOrderDetail' => ['goodId'] , // 卖出-团购-收货信息
        'teamDetail' => ['team_id']
    ];


}

封装的BaseValidate  :

validate:


// +----------------------------------------------------------------------

namespace think;

use think\exception\ClassNotFoundException;
use think\validate\ValidateRule;

class Validate
{

    /**
     * 自定义验证类型
     * @var array
     */
    protected static $type = [];

    /**
     * 验证类型别名
     * @var array
     */
    protected $alias = [
        '>' => 'gt', '>=' => 'egt', '<' => 'lt', '<=' => 'elt', '=' => 'eq', 'same' => 'eq',
    ];

    /**
     * 当前验证规则
     * @var array
     */
    protected $rule = [];


    /**  











    /**
     * 数据自动验证
     * @access public
     * @param  array     $data  数据
     * @param  mixed     $rules  验证规则
     * @param  string    $scene 验证场景
     * @return bool
     */
    public function check($data, $rules = [], $scene = '')
    {
        $this->error = [];

        if (empty($rules)) {
            // 读取验证规则
            $rules = $this->rule;
        }

        // 获取场景定义
        $this->getScene($scene);

        foreach ($this->append as $key => $rule) {
            if (!isset($rules[$key])) {
                $rules[$key] = $rule;
                unset($this->append[$key]);
            }
        }

        foreach ($rules as $key => $rule) {
            // field => 'rule1|rule2...' field => ['rule1','rule2',...]
            if (strpos($key, '|')) {
                // 字段|描述 用于指定属性名称
                list($key, $title) = explode('|', $key);
            } else {
                $title = isset($this->field[$key]) ? $this->field[$key] : $key;
            }

            // 场景检测
            if (!empty($this->only) && !in_array($key, $this->only)) {
                continue;
            }

            // 获取数据 支持多维数组
            $value = $this->getDataValue($data, $key);

            // 字段验证
            if ($rule instanceof \Closure) {
                $result = call_user_func_array($rule, [$value, $data, $title, $this]);
            } elseif ($rule instanceof ValidateRule) {
                //  验证因子
                $result = $this->checkItem($key, $value, $rule->getRule(), $data, $rule->getTitle() ?: $title, $rule->getMsg());
            } else {
                $result = $this->checkItem($key, $value, $rule, $data, $title);
            }

            if (true !== $result) {
                // 没有返回true 则表示验证失败
                if (!empty($this->batch)) {
                    // 批量验证
                    if (is_array($result)) {
                        $this->error = array_merge($this->error, $result);
                    } else {
                        $this->error[$key] = $result;
                    }
                } else {
                    $this->error = $result;
                    return false;
                }
            }
        }

        return !empty($this->error) ? false : true;
    }





测试:

在thinkphp5.1 自定义验证规则 获取get 传递的值的时候 传递了 值 能够获取到 验证出错_第1张图片

打印获取的参数:

 public function teamDetail(){
       if(request()->isGet()){
           $team_id = $this->request->get('team_id', '');


           dump($team_id);die();
           $this->validate->scene('teamDetail')->check($team_id);
           if ($this->validate->getError()) {
               return resultArray(lang(strval($this->validate->getError())));
           }
           $page = $this->request->param('page', '1');
           $limit = $this->request->param('limit', '10');
           return $this->logic->teamDetail($team_id,$page,$limit);
       }
       return '';
    }

 

结果:

在thinkphp5.1 自定义验证规则 获取get 传递的值的时候 传递了 值 能够获取到 验证出错_第2张图片

你可能感兴趣的:(前端,javascript,开发语言)