Tp-validate進階thinkphp

 

 

階段1:基礎php

application/controller/v1/Banner.phpthinkphp

<?php
namespace app\api\controller\v1;
use think\Controller;
use think\validate;

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());//返回錯誤信息

    } 

}

階段二:講=將驗證規則單獨放到其餘的類文件中api

<?php
namespace app\api\controller\v1;
use think\Controller;
use think\validate;
//use app\api\validate\TestValidate;
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 \app\api\validate\TestValidate();

         //或者引入命名空間在new use app\api\validate\TestValidate (application/api/validate/TestValidate.php)
       $validate= new TestValidate();//必須有這個命名空間 use app\api\validate\TestValidate
        //batch()批量驗證 不然只返回最後一個getError驗證信息
        $result=$validate->batch()->check($data);//返回布爾
        var_dump($result);
        var_dump($validate->getError());//返回錯誤信息

    } 

}
application/api/validate/TestValidate.php
<?php
namespace app\api\validate;
use think\Validate;
class TestValidate extends Validate{
    protected $rule =[
        'name'=>'require|max:10',
        'email'=>'email'        

    ];
}

 

階段三:封裝驗證參數:app

 application/controller/v1/Banner.phpthinkphp5

<?php
namespace app\api\controller\v1;
use think\Controller;
use think\validate;
use app\api\validate\IDMustBePositiveInt;
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.phpui

<?php
namespace app\api\validate;
use think\Request;
use think\Validate;
use think\Exception;
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.phpthis

 

<?php
namespace app\api\validate;
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規則就好了spa

階段5:建立更多的自定義驗證類code

階段六:自定義驗證類文件多了,就須要工廠類來封裝啦!blog

相關文章
相關標籤/搜索