zoukankan      html  css  js  c++  java
  • TP5 自定义验证器

    TP内置验证功能提供两种验证方法

    • 验证器(推荐)
    $validate = Validate::make([
        'id' => 'require|integer',
    ]);
    if ($validate->check(['id' => $id])) {
        
    }

    优点: 方便、快捷

    缺点: 可读性和扩展性差

    • 独立验证
    namespace appadminvalidate;
    class Banner extends BaseValidate {
        protected $rule = [
            'name' => 'require|max:25',
            'age' => 'number|between:1,120',
            'email' => 'email',
        ];
        protected $message = [
            'name.require' => '名称必须',
            'name.max' => '名称最多不能超过25个字符',
            'age.number' => '年龄必须是数字',
            'age.between' => '年龄只能在1-120之间',
            'email' => '邮箱格式错误',
        ];
        protected $scene = [
            'edit' => ['name','age'],
        ];
    }

    官方提供了一些验证规则,如果不能满足需求可以自定义

    建议自定义在验证器基类中,这样所有继承这个类的子验证器都可以使用这些验证方法

    定义规则

        protected function isMobile($value) {
            $rule = '^1(3|4|5|7|8)[0-9]d{8}$^';
            $result = preg_match($rule, $value);
            if ($result) {
                return true;
            } else {
                return false;
            }
        }

     自定义验证器有以下几个参数

    value:待验证的值

    field:验证字段名

    验证器推荐使用方法

    定义验证器基类,继承 Validate类 ,在基类中定义一些公共验证规则和方法

    <?php
    namespace appadminvalidate;
    use thinkValidate;
    
    /**
     * Class BaseValidate
     * 验证类的基类
     */
    class BaseValidate extends Validate {
        /**
         * 检测所有客户端发来的参数是否符合验证类规则
         * 基类定义了很多自定义验证方法
         * 这些自定义验证方法其实,也可以直接调用
         * @throws ParameterException
         * @return true
         */
        public function goCheck() {
            $params = request()->param();
            if (!$this->check($params)) {
                return $this->error;
            }
            return true;
        }
    
        /**
         * @param array $arrays 通常传入request.post变量数组
         * @return array 按照规则key过滤后的变量数组
         * @throws ParameterException
         */
        public function getDataByRule($arrays) {
            if (array_key_exists('user_id', $arrays) | array_key_exists('uid', $arrays)) {
                return '参数中包含有非法的参数名user_id或者uid';
            }
            $newArray = [];
            foreach ($this->rule as $key => $value) {
                $newArray[$key] = $arrays[$key];
            }
            return $newArray;
        }
    
        protected function isPositiveInteger($value, $rule = '', $data = '', $field = '') {
            if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) {
                return true;
            }
            return $field . '必须是正整数';
        }
    
        protected function isNotEmpty($value, $rule = '', $data = '', $field = '') {
            if (empty($value)) {
                return $field . '不允许为空';
            } else {
                return true;
            }
        }
    
        //没有使用TP的正则验证,集中在一处方便以后修改
        //不推荐使用正则,因为复用性太差
        //手机号的验证规则
        protected function isMobile($value) {
            $rule = '^1(3|4|5|7|8)[0-9]d{8}$^';
            $result = preg_match($rule, $value);
            if ($result) {
                return true;
            } else {
                return false;
            }
        }
    
    }
  • 相关阅读:
    《数据通信与网络》笔记--数据链路层的成帧
    设计模式10---设计模式之原型模式(Prototype)
    Yii 控制dropdownlist / select 控件的宽度和 option 的宽度
    [置顶] 如何vs在cocos2dx项目中打印中文
    mongodb实现简单的增删改查
    北京和硅谷在创新方面的区别
    Android 解决Gallery下ScrollView滑动事件冲突
    Java 授权内幕--转载
    JAVA 上加密算法的实现用例---转载
    基于事件的 NIO 多线程服务器--转载
  • 原文地址:https://www.cnblogs.com/xiaoliwang/p/9344875.html
Copyright © 2011-2022 走看看