zoukankan      html  css  js  c++  java
  • 封装了一个验证类

    <?php
    /**
     * 表单验证工具类
     * User: Eden
     * Date: 19-4-26 上午9:23
     */
    
    namespace CommonUtil;
    
    use VendorFuncJson;
    use VendorFuncVerify;
    
    class ValidUtil extends CommonUtil {
        /**
         * @param $regulations
         * @param $params
         * @return bool
         */
        public static function each($regulations,$params) {
            //$regulations = [
            //    'name' => [
            //        'required' => '请先写姓名|10001',
            //    ],
            //    'telephone' => [
            //        'required' => '请先写手机号|10001',
            //        'telephone' => '请先写正确的手机号|10002',
            //    ],
            //    'id_number' => [
            //        'required' => '请填写身份证号|10001',
            //        'id_card' => '请填写正确的身份证号|10001',
            //    ],
            //    'department_id' => [
            //        'required' => '请选择科室|10001',
            //    ],
            //    'uid' => [
            //        'required' => '缺少用户id|10001',
            //    ],
            //];
            $json = new Json();
            foreach ($regulations as $param => $regulation) {
                foreach ($regulation as $rule => $tips) {
                    if (((string)$rule === 'required') && !$params[$param]) {
                        $tips = explode('|',$tips);
                        $json->printOutError($tips[0],$tips[1] ?:'10001');
                        break;
                    }
    
                    if (((string)$rule === 'telephone') && !Verify::checkMobile($params[$param])) {
                        $tips = explode('|',$tips);
                        $json->printOutError($tips[0],$tips[1] ?:'10001');
                        break;
                    }
    
                    if (((string)$rule === 'id_card') && !Verify::checkIDCard($params[$param])) {
                        $tips = explode('|',$tips);
                        $json->printOutError($tips[0],$tips[1] ?:'10001');
                        break;
                    }
                }
            }
    
            return true;
        }
    }
    

    使用如下,

    $regulations = [
        'name' => [
            'required' => '请填写姓名|10001',
        ],
        'telephone' => [
            'required' => '请填写手机号|10001',
            'telephone' => '请填写正确的手机号|10002',
        ],
        'id_number' => [
            'required' => '请填写身份证号|10001',
            'id_card' => '请填写正确的身份证号|10001',
        ],
        'department_id' => [
            'required' => '请选择科室|10001',
        ],
        'uid' => [
            'required' => '缺少用户id|10001',
        ],
    ];
    $params = $_POST;
    ValidUtil::each($regulations,$params);
    

    通用的验证,就交给它。
    跟数据库交互的验证,在下面的步骤中,继续进行。

  • 相关阅读:
    Nginx 允许多个域名跨域访问
    mongo 命令
    PyTorch torch.cuda.device_count 返回值与实际 GPU 数量不一致
    APUE 学习笔记 —— 文件I/O
    Django transaction 误用之后遇到的一个问题与解决方法
    如何更新 CentOS 镜像源
    Supervisor 的安装与配置教程
    Sentry的安装搭建与使用
    Python, Django 性能分析工具的使用
    记一次 Apache HUE 优化之因使用 Python 魔术方法而遇到的坑
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/10856530.html
Copyright © 2011-2022 走看看