Blogger Information
Blog 61
fans 0
comment 0
visits 62933
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
thinkPHP5.1中的验证和独立验证
Pengsir
Original
1661 people have browsed it

用户自定义验证类User:

<?php
/**
 * 验证规则,就是一个类
 */

namespace app\common;

use think\Validate;

class User extends Validate
{
    //系统框架里的Validate的验证规则
    //当前验证规则
    protected $rule = [
            'name|姓名'=>[
                'require'=>'require',//必填,不能为空
                'min'=>3,//最小为3位
                'max'=>10,//最大为10位
            ],
        'email|邮箱'=>[
            'require'=>'require',
            'email'=>'email'
        ],
        'mobile|手机'=>[
            'require'=>'require',
            'mobile'=>'mobile'
        ],
        'password|密码'=>[
            'require'=>'require',
           'min'=>6,
            'max'=>12,
            'alphaNum'=>'alphaNum'//仅允许字母+数字
        ]
    ];
}

静态代理User类:

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/24 0024
 * Time: 17:37
 */

namespace app\facade;


use think\Facade;

class User extends Facade
{
    //第二种方式  用静态代理调用
        //获取当前Facade对应类名
    protected static function getFacadeClass()
    {
        return 'app\common\User';
    }
}

控制器下Demo9代码:

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/23 0023
 * Time: 21:44
 */

namespace app\index\controller;


use think\Controller;
use app\common\User;
//use app\facade\User;//导入静态代理类

use think\facade\Validate;

class Demo9 extends Controller
{
    //第一种方法
    public function test1(){
        //要验证的数据
        $data = [
            'name'=>'peng',
            'email'=>'peng@qq.com',
            'mobile'=>'18871587950',
            'password'=>'123abc'
        ];
        //创建User这个类的实例
        $validate = new User();

         if(!$validate->check($data)){
             return $validate->getError();
         }
             return '验证通过';
         }
         //第二种方法,用静态代理调用
         public function test2(){
             //要验证的数据
             $data = [
                 'name'=>'peng',
                 'email'=>'peng@qq.com',
                 'mobile'=>'18871587950',
                 'password'=>'123abc'
             ];
             if(!User::check($data)){
                 return User::getError();
             }
             return '验证通过';
         }
         //第三种方法,在控制中调用,因为控制中有验证数据的方法
            public function test3(){
                //3.1要验证的数据
                $data = [
                    'name'=>'peng',
                    'email'=>'peng@qq.com',
                    'mobile'=>'18871587950',
                    'password'=>'123abc'
                ];
                    //3.2 验证器名
                $validate = 'app\common\User';

                //3.3 验证方法
               // protected function validate($data, $validate)
                $res = $this->validate($data, $validate);
                if($res!==true){
                    return $res;
                }
                    return '验证通过';
                }
        /*
         * 验证总结:
         * 1.验证器是一个自定义的类,必须继承框架的验证类think\Validate.php
         * 2.验证器可以创建在应用application目录下的任何一个可以访问的目录下面
         *    这个访问是指控制器可以访问,并不是指外部的URL访问,只需要指定正确的命名空间
         * 3.验证器其实就是完成框架的think\Validate类中的属性protected $rule=[] 初始化
         * 4.在控制器中直接实例化调用check()完成验证
         * 5.还可以创建一个自定义的静态代理,来统一验证方法的调用方式
         */


        /*
         * 独立验证总结:
         *  原理:就是单独用Validate这个类调用自己的非静态方法check($data,$rule)
         * 1.把这个类变成静态的,就要引入use think\facade\Validate;facade\Validate里的方法把Validate类,类似转为静态
         * 2.Validate类调check($data,$rule)
         */
    //        2.独立验证:使用的是验证器类的think\Validate中的rule()方法
            //rule()方法实际上就是完成给当前类的protected $rule =[]初始化
            //独立验证就是不依赖于用户自定义的验证器类:app\common\User.php
        public function test4(){
            /*
             * 这只是添加字段的验证规则
             *string|array  $name  字段名称或者规则数组
             *$rule  验证规则或者字段描述信息
             * public function rule($name, $rule = '')
             */
            //4.1要验证的数据
            $data = [
                'name'=>'peng',
                'email'=>'peng@qq.com',
                'mobile'=>'18871587950',
                'password'=>'123abc'
            ];

            $rule = [
                'name|姓名'=>[
                    'require'=>'require',//必填,不能为空
                    'min'=>3,//最小为3位
                    'max'=>10,//最大为10位
                ],
                'email|邮箱'=>[
                    'require'=>'require',
                    'email'=>'email'
                ],
                'mobile|手机'=>[
                    'require'=>'require',
                    'mobile'=>'mobile'
                ],
                'password|密码'=>[
                    'require'=>'require',
                    'min'=>6,
                    'max'=>12,
                    'alphaNum'=>'alphaNum'//仅允许字母+数字
                ]
            ];
            //添加字段的验证规则:初始化rule属性
            Validate::rule($rule);

           if(!Validate::check($data)){
               return Validate::getError();
           }
           return '验证通过';
        }
    }


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post