tp5.1验证器的使用

栏目: PHP · 发布时间: 6年前

内容简介:一直以来都是对着手册写验证器,感觉没有学习到精髓,今天系统的研究了一下,本文记录一下。场景如下:对一个Login控制器设计验证器进行验证,步骤如下:

一直以来都是对着手册写验证器,感觉没有学习到精髓,今天系统的研究了一下,本文记录一下。

场景如下:

对一个Login控制器设计验证器进行验证,步骤如下:

1、新建一个名为Login的验证器,并添加验证规则:

<?php

namespace app\index\validate;

use think\Validate;

class Login extends Validate
{
    protected $rule = [
        'username' => 'require',
        'password' => 'require',
        'captcha'  => 'require|captcha',
    ];

    protected $message = [
        'username.require' => '请输入账号',
        'password.require' => '请输入密码',
        'captcha.require'  => '请输入验证码',
        'captcha.captcha'  => '验证码错误',
    ];
}

接着在Login控制器中添加:

if ($this->request->isPost()) {
            $param = $this->request->param();
            $result = $this->validate($param, 'login');
            if ($result !== true) {
                $this->error($result);
            }

使用validate方法可以简化验证,第二个参数指定的是验证器,不区分大小写

参考资料:https://www.kancloud.cn/manual/thinkphp5_1/354101


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Text Algorithms

Text Algorithms

Maxime Crochemore、Wojciech Rytter / Oxford University Press / 1994

This much-needed book on the design of algorithms and data structures for text processing emphasizes both theoretical foundations and practical applications. It is intended to serve both as a textbook......一起来看看 《Text Algorithms》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具