TP5 validate验证机制
首先使用验证器验证,在模块目录下,建一个validate目录,在其下面自定义要使用的验证器,并让它extends 框架的Validate类:
<?php namespace app\admin\validate;use think\Validate;class User extends Validate{protected $rule = ['user_name' => 'require|max:20','password' => 'require','real_name' => 'require|max:12','email' => 'email','qq' => 'max:20','phone' => 'require|length:11',]; }
这样我们就有了一个针对user使用的自动验证器,我们将要验证的字段,给他定义好对应的规则。
这里注意:框架本身自带的很多规则及其提示信息,直接调用就好,如果不满足需求,还支持自定义规则。
贴出框架源代码:
// 验证规则默认提示信息 protected static $typeMsg = ['require' => ':attribute require','number' => ':attribute must be numeric','integer' => ':attribute must be integer','float' => ':attribute must be float','boolean' => ':attribute must be bool','email' => ':attribute not a valid email address','mobile' => ':attribute not a valid mobile','array' => ':attribute must be a array','accepted' => ':attribute must be yes,on or 1','date' => ':attribute not a valid datetime','file' => ':attribute not a valid file','image' => ':attribute not a valid image','alpha' => ':attribute must be alpha','alphaNum' => ':attribute must be alpha-numeric','alphaDash' => ':attribute must be alpha-numeric, dash, underscore','activeUrl' => ':attribute not a valid domain or ip','chs' => ':attribute must be chinese','chsAlpha' => ':attribute must be chinese or alpha','chsAlphaNum' => ':attribute must be chinese,alpha-numeric','chsDash' => ':attribute must be chinese,alpha-numeric,underscore, dash','url' => ':attribute not a valid url','ip' => ':attribute not a valid ip','dateFormat' => ':attribute must be dateFormat of :rule','in' => ':attribute must be in :rule','notIn' => ':attribute be notin :rule','between' => ':attribute must between :1 - :2','notBetween' => ':attribute not between :1 - :2','length' => 'size of :attribute must be :rule','max' => 'max size of :attribute must be :rule','min' => 'min size of :attribute must be :rule','after' => ':attribute cannot be less than :rule','before' => ':attribute cannot exceed :rule','expire' => ':attribute not within :rule','allowIp' => 'access IP is not allowed','denyIp' => 'access IP denied','confirm' => ':attribute out of accord with :2','different' => ':attribute cannot be same with :2','egt' => ':attribute must greater than or equal :rule','gt' => ':attribute must greater than :rule','elt' => ':attribute must less than or equal :rule','lt' => ':attribute must less than :rule','eq' => ':attribute must equal :rule','unique' => ':attribute has exists','regex' => ':attribute not conform to the rules','method' => 'invalid Request method','token' => 'invalid token','fileSize' => 'filesize not match','fileExt' => 'extensions to upload is not allowed','fileMime' => 'mimetype to upload is not allowed', ];
我也没一个一个试额。
验证器有了,接下来就要使用了哈,嗯,文档中叫验证情景,不对,是场景:
我是在user控制器中验证,所以我写在控制器中:
$validate = Loader::validate('User'); if(!$validate->check($data)){dump($validate->getError()); }else{dump($data); }
加载自定义好的验证器,然后使用check()方法。对传入的数据进行验证。失败则获取error,成功则输出!
当然,tp5提供一个实例化验证器的助手函数:
$validate = validate("User");
我不喜欢用助手函数,不过功能实现就好。
不使用验证器,直接在场景使用时定义规则,就是独立验证。
在实例化validate对象的时候,传入你定义的规则即可,也可以使用rule()方法定义。写法不同罢了。
同样,上面所用的采用'|'来添加多个规则的写法,也可以改为数组的方法,数组无处不在。。。
如果要改变提示信息,也可以在验证器中自定义:
<?php namespace app\admin\validate;use think\Validate;class User extends Validate{protected $rule = ['user_name' => 'require|max:20','password' => 'require','real_name' => 'require|max:12','email' => 'email','qq' => 'number|max:20','phone' => 'require|length:11|number',];protected $message = ['user_name.require' => '名称必须','user_name.max' => '名称最多不能超过25个字符','qq.number' => 'qq必须是数字','phone.number' => '手机必须是数字','phone.max' => '请输入11位手机号','email' => '邮箱格式错误',];}
控制器中使用批量验证的话,我木有试额,批量验证不通过返回的是一个错误数组:
$validate = new Validate($rule, $msg);
$result = $validate->batch()->check($data);
”
上面一直使用的是框架自带的规则,定义验证规则:
1、使用了验证器的话,只要在类中,定义函数
'user_name' => 'require|max:20|checkName:xinghua',
自定义一个checkName规则:
// 自定义验证规则 protected function checkName($value,$rule,$data){return $rule == $value ? true : '名称必须是xinghua'; }
定义的规则名不能和已有的冲突哦;
2、没使用验证器的话,采用extend()方法定义
$validate->extend('checkName', function ($value, $rule) {
return $rule == $value ? true : '名称错误';
});
还支持批量注册规则,同样是使用数组。
定义字段信息,可以给对应的字段添加描述:
protected $field = ['user_name' => '用户名','password' => '密码','real_name' => '姓名','email' => '邮箱','qq' => 'QQ','phone' => '手机' ];
在使用场景中,直接定义字段,来限制当前验证的字段。
在验证器中定义$scene:
protected $scene = ['register' => ['user_name','real_name','password','phone','email'], ];
场景使用时,就需要加上scene()方法:
$validate->scene('register')->check($data)
显然,我没把qq字段写进去,设定的qq验证规则木有生效。
在scene里还可以重写规则,
protected $scene = [
'edit' => ['name','age'=>'require|number|between:1,120'],
];
动态设置的话采用匿名函数:$validate->scene('edit', function($key,$data){
return 'email'==$key && isset($data['id'])? true : false;
});
END;
TP5 validate验证机制相关推荐
- MVC与Validate验证提示的样式修改
MVC中使用Validate的验证,要修改错误提示样式一共有3处需要修改,否则就不太完美了: MVC中的Validate的验证机制只用在后台写一次,就可以完成前台和后台的完美验证,前台的验证主要是依靠 ...
- 如何利用azMan (Authorization Manager) 实现 role-based的安全验证机制
在WCF中如何配置基于asp.net role的授权机制,看了些时日,总算有点眉目了 . 以下是一个典型的通过自定义的role-based (principalPermissionMode=UseAs ...
- 一个用户实体应该有它自己的实体验证,即一个实体应该有它的属性,方法,扩展属性及验证机制组成...
一个存储验证信息的公用类: /// <summary> /// 验证信息实体类 /// </summary> public class RuleV ...
- oauth最后的确认按钮_绕过GitHub的OAuth授权验证机制($25000)
这几年来,信息安全研究一直是我的业余爱好,虽然有很多人专职做漏洞众测以获得奖励,但对我个人来说,我只对一些感兴趣的项目投入不多的时间去深入研究.今年,我想看看自己是否是全职漏洞赏金猎人的料,所以就从6 ...
- jQuery Validate验证框架详解
2019独角兽企业重金招聘Python工程师标准>>> 一.导入js库 <script type="text/javascript" src="& ...
- Uchome的登录验证机制
2019独角兽企业重金招聘Python工程师标准>>> 登录: 成功后设置cookie //设置cookie ssetcookie('auth', authcode("$s ...
- 使用RMAN VALIDATE验证数据和备份
在oracle中可以使用rman VALIDATE来检查数据库是否存在坏块,检测备份集是否有用, 特别是备份集,建议定期做VALIDATE 验证,避免备份失败造成数据库损失. 1.VALIDATE D ...
- QT开发(六十六)——登录对话框的验证机制
QT开发(六十六)--登录对话框的验证机制 一.验证码机制 为了避免被恶意程序***,程序通常要使用安全机制.验证码机制是提供产生随机验证码,由用户识别填写来判断用户有效性的安全机制. 验证码必须动态 ...
- MVC中的统一验证机制~续
前段时间我发表的关于MVC架构中对验证方式的设计,收到了不少朋友的留言,意思是说过于复杂,复用性不高,当然我的出发点是减少实体部门的代码量. 最近在朋友的建议下,看了另一种验证方式,事实上就是MVC实 ...
最新文章
- SDK安装报错HTTP Status 416
- sklearn使用投票回归VotingRegressor算法构建多模型融合的投票回归模型、并自定义子回归器的权重(weights)、评估多模型融合的回归模型、评估R2、mse、rmse、mape
- 菜鸟程序员的成长之路-工作篇
- 数据库拆分过程及挑战
- IDEA无法识别pom.xml文件,内容全部显示为灰色,或者无颜色
- python常用的十进制、16进制之间的转换
- rust(26)-单元类型与never
- Arrays.copyOf()、Arrays.copyOfRange()与System.arraycopy()用法
- Mybatis if test 中int integer判断非空的坑
- Jqgried树形列表
- 利用指针编程实现:删除一个字符串中的所有空格 c语言,C语言必考100题解析汇报...
- Opencv 图像入门一之基本操作
- SqlServer动态表查询
- oopc——5.多态
- 【转】自底向上和自顶向下的区别
- 神经网络有趣案例_求解三体问题快了1亿倍,新型神经网络问世
- python dll load failed_python安装MySQLdb的问题 ImportError: DLL load failed
- java链式存储_Java实现链式存储的二叉树
- Linux操作系统下的多线程编程详细解析----条件变量
- 软件测试类型方法步骤英语,软件测试类英文面试题
热门文章
- 自然语言处理顶会ACL 2020会议核心要点分享
- VCC AVCC VDD AVDD区别
- 字符串映射类not support type错误
- 计算机水平题目,2017年全国计算机等级考试试题操作题.doc
- AndroidStudio 和 IDEA 的 Favorites 及 Bookmarks 数据找回办法
- 【Python精华】100个Python练手小程序(Python3 已亲测)
- 图片坐标提取软件/图片坐标点和像素点颜色提取软件/图片坐标获取工具/Python图片坐标获取源码/图片像素坐标获取软件/python tkinter 图片显示(完全开源)
- Linux DRM(四) -- loongson driver
- 仙人掌之歌——跳槽前后(5)
- 学生会办公室专用信息收集软件1.6版