内容简介:一直以来都是对着手册写验证器,感觉没有学习到精髓,今天系统的研究了一下,本文记录一下。场景如下:对一个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
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 使用装饰器完成用户身份验证
- python使用Tesseract库识别验证
- 使用node+puppeteer破解验证码
- 使用REST Assured测验验证REST服务
- 使用.Net Core实现的一个图形验证码
- 使用Testinfra和Ansible验证服务器状态
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Two Scoops of Django
Daniel Greenfeld、Audrey M. Roy / CreateSpace Independent Publishing Platform / 2013-4-16 / USD 29.95
Two Scoops of Django: Best Practices For Django 1.5 is chock-full of material that will help you with your Django projects. We'll introduce you to various tips, tricks, patterns, code snippets, and......一起来看看 《Two Scoops of Django》 这本书的介绍吧!