tp5.1验证器的使用

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

内容简介:一直以来都是对着手册写验证器,感觉没有学习到精髓,今天系统的研究了一下,本文记录一下。场景如下:对一个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


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

查看所有标签

猜你喜欢:

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

Building Web Reputation Systems

Building Web Reputation Systems

Randy Farmer、Bryce Glass / Yahoo Press / 2010 / GBP 31.99

What do Amazon's product reviews, eBay's feedback score system, Slashdot's Karma System, and Xbox Live's Achievements have in common? They're all examples of successful reputation systems that enable ......一起来看看 《Building Web Reputation Systems》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

随机密码生成器
随机密码生成器

多种字符组合密码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具