tp5.1验证器的使用

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

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


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

查看所有标签

猜你喜欢:

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

Data Structures and Algorithm Analysis in Java

Data Structures and Algorithm Analysis in Java

Mark A. Weiss / Pearson / 2011-11-18 / GBP 129.99

Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course wa......一起来看看 《Data Structures and Algorithm Analysis in Java》 这本书的介绍吧!

html转js在线工具
html转js在线工具

html转js在线工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具