zoukankan      html  css  js  c++  java
  • Tp-validate进阶thinkphp

    阶段1:基础

    application/controller/v1/Banner.php

    <?php
    namespace appapicontrollerv1;
    use thinkController;
    use thinkvalidate;
    
    class Banner extends controller{
        public function index(){
            //http://localhost/thinkphp5/public/index.php/api/v1.Banner/index
    
        }
        public function  getBanner(){
            
            $data=array(
                'name'=>'dash',
                'email'=>'wolichihua2011@163.com'
    
            );
            //独立验证
            $validate=new Validate([
    
                'name'=>'require|max:10',
                'email'=>'email'
            ]);
    
            //batch()批量验证 否则只返回最后一个getError验证信息
            $result=$validate->batch()->check($data);//返回布尔
            var_dump($result);
            var_dump($validate->getError());//返回错误信息
    
        } 
    
    }

    阶段二:讲=将验证规则单独放到其他的类文件中

    <?php
    namespace appapicontrollerv1;
    use thinkController;
    use thinkvalidate;
    //use appapivalidateTestValidate;
    class Banner extends controller{
        public function index(){
            //http://localhost/thinkphp5/public/index.php/api/v1.Banner/index
        }
        public function  getBanner($id){
            
            $data=array(
                'name'=>'dash',
                'email'=>'wolichihua2011@163.com'
    
            );
    
            //验证器 直接new 
            $validate= new appapivalidateTestValidate();
    
             //或者引入命名空间在new use appapivalidateTestValidate (application/api/validate/TestValidate.php)
           $validate= new TestValidate();//必须有这个命名空间 use appapivalidateTestValidate
            //batch()批量验证 否则只返回最后一个getError验证信息
            $result=$validate->batch()->check($data);//返回布尔
            var_dump($result);
            var_dump($validate->getError());//返回错误信息
    
        } 
    
    }
    application/api/validate/TestValidate.php
    <?php
    namespace appapivalidate;
    use thinkValidate;
    class TestValidate extends Validate{
        protected $rule =[
            'name'=>'require|max:10',
            'email'=>'email'        
    
        ];
    }

    阶段三:封装验证参数:

     application/controller/v1/Banner.php

    <?php
    namespace appapicontrollerv1;
    use thinkController;
    use thinkvalidate;
    use appapivalidateIDMustBePositiveInt;
    class Banner extends controller{
        public function index(){
            http://localhost/thinkphp5/public/index.php/api/v1.Banner/index
        }
        public function  getBanner($id){
            
            (new IDMustBePositiveInt())->goCheck();
    
        } 
    
    }

    application/api/validate/BaseValidate.php

    <?php
    namespace appapivalidate;
    use thinkRequest;
    use thinkValidate;
    use thinkException;
    class BaseValidate extends Validate{
        public function goCheck(){
            // 获取http参数
            // 对这些参数做检验
            $request= Request::instance();
            $params=$request->param();
            $result=$this->check($params);
            if (!$result) {
                $error=$this->error;
                throw new Exception($error, 1);
                
            }else{
                return true;
            }
        }
    }

    application/api/validate/IDMustBePositiveInt.php

    <?php
    namespace appapivalidate;
    class IDMustBePositiveInt extends BaseValidate{
        protected $rule=array(
    
            'id'=>'require|isPositiveInteger'
        );
    
        //自定义验证规则
        protected function isPositiveInteger($value, $rule='', $data='', $field='')
        {
            if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) {
                return true;
            }
            return $field . '必须是正整数';
        }
    }

    阶段4:讲自定义规则挪到BaseValidate.php中,其他自定义的也一样只保留rule规则就行了

    阶段5:创建更多的自定义验证类

    阶段六:自定义验证类文件多了,就需要工厂类来封装啦!

  • 相关阅读:
    偶然看到3年前为企业内刊写的文章——像经营自己的事业一样经营工作
    Migration 时需要留意到几个问题
    Law of Seed
    时刻准备着
    一种trace APP Engine的方法
    强烈建议博客园增加Oracle ERP或者叫PeopleSoft模块
    回味四年前的自己——细节的魅力
    一封寫給兒子的信
    职场回顾:面试精华试题大全[转]
    快速放松的11种简单方法
  • 原文地址:https://www.cnblogs.com/lichihua/p/9697354.html
Copyright © 2011-2022 走看看