Blogger Information
Blog 29
fans 0
comment 0
visits 27239
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
thinkPHP5.1中的验证和独立验证
LIWEN的博客
Original
981 people have browsed it

文档结构如下图:

2018-01-24_111004.png

控制中包含两个方法,test1()在调用User类中的验证规则进行验证,test3()将验证规则写在控制器中直接验证,称为独立验证。

控制器代码如下:

<?php
namespace app\index\controller;
use app\validate\User;
use think\Controller;
//use app\facade\User;   //采用静态代理方式调用时使用
//use think\facade\Validate;  //采用静态代理方式调用时使用

class Demo9 extends Controller
{
    public function test1()
    {
        //准备验证数据
        $data = [
            'name' => 'Peter',
            'email' => 'p@php.cn',
            'password' => '123abc',
            'mobile' => '18911112222'
        ];

        //验证器
        //第一种方法:
        $validate = new User();
        if (!$validate->check($data)){
            return $validate->getError();
        }
        return '验证通过';

        //第二种方法:静态代理
//        if (!User::check($data)){
//            return User::getError();
//        }
//        return '验证通过';
    }

    public function test3()
    {
        //创建验证规则
        $rule = [
            'name|<span style="color: red">姓名</span>' =>[
                'require',
                'min' => 5,
                'max' =>10,
            ],
            'email|<span style="color: red">邮箱</span>' => [
                'require',
                'email' => 'email',
            ],
            'password|<span style="color: red">密码</span>' => [
                'require',
                'min' => 3,
                'max' => 10,
                'alphaNum'
            ],
            'mobile|<span style="color: red">手机</span>' => [
                'require',
                'mobile'
            ]

        ];
        //添加字段的验证规则:初始化rule属性
        Validate::rule($rule);
        //准备要验证的数据
        $data = [
            'name' => 'Peter',
            'email' => 'p@php.cn',
            'password' => '123abc',
            'mobile' => '18911112222'
        ];
        //如果验证不通过,直接输出错误
        if (!Validate::check($data)){
            return Validate::getError();
        }
        return '<h3 style="color: green" >验证通过!~</h3>';

    }
}

User类文件名为User.php,代码内容:

<?php

namespace app\validate;
use think\Validate;

class User extends Validate
{
    //当前验证的规则
    protected $rule = [
        'name|姓名' =>[
            'require',
            'min' => 5,
            'max' =>10,
        ],
        'email|邮箱' => [
            'require',
            'email' => 'email',
        ],
        'password|密码' => [
            'require',
            'min' => 3,
            'max' => 10,
            'alphaNum'
        ],
        'mobile|手机' => [
            'require',
            'mobile'
        ]

    ];

}

User类的静态代理代码如下:

<?php

namespace app\facade;
use think\Facade;

class User extends Facade
{
    protected static function getFacadeClass()
    {
        return 'app\validate\User';
    }
}


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