验证器和独立验证的使用

Original 2019-04-21 11:27:44 367
abstract:<?phpnamespace app\index\controller;use think\Controller;use app\Validate\Task;   //导入验证器use think\Validate;class Test extends Controller{    //验证器验证    public functio

<?php

namespace app\index\controller;


use think\Controller;

use app\Validate\Task;   //导入验证器

use think\Validate;

class Test extends Controller

{

    //验证器验证

    public function task1()

    {

    $infor=[

            'name'=> 'apio',

            'sex'=> 1,

            'age'=> 18,

            'phone'=> 13576868462,

            'QQ'=> 12346789

    ];

    $validate= new Task();

    if(!$validate->check($infor)){

    dump($validate->getError());

        }

        return '验证成功!';

       

    }


    public function task2()

    {

    $infor=[

            'name'=> 'apio',

            'sex'=> 1,

            'age'=> 18,

            'phone'=> 13576868462,

            'QQ'=> 12346789

    ];

    $rule='app\validate\Task';

    $rap = $this->validate($infor,$rule);

    if($rap !== true){

    return $rap;

    }

    return '验证成功!';

    }


    public function task3()

    {

    $a=['age'=>17];

    $b=['age'=>'between:18,40'];

    $c=['age.between'=>'年龄必须在18到40之间'];

    $d=$this->validate($a,$b,$c);

    if($d !== true){

    return $d;

    }

    return '验证成功!';

    }


    //独立验证

    public function task4()

    {

        $a=['QQ'=>'require|length:5,10'];

        

        $b=[

            'QQ.require'=>'QQ号码不能为空',

            'QQ.between'=>'QQ号码必须在5到10个数字之间'

           ];

        

        $c=['QQ'=>123456789];

        

        $d=validate::make($a,$b);

        

        $e=$d->check($c);

        

        return $e ? '验证通过': $d->getError();

    }

}

----------------------------------------------------------------------------------------------------

Correcting teacher:天蓬老师Correction time:2019-04-22 10:01:50
Teacher's summary:独立验证是所有其它验证的基础 , 不论是验证器, 还是控制器的验证快捷方式, 都是要依赖框架中的验证类完成的...

Release Notes

Popular Entries