定义验证器类:

namespace app\index\validate;
use think\Validate;
class User extends Validate
{
protected $rule = [
'name' => 'require|max:25',
'email' => 'email',
]; protected $message = [
'name.require' => '用户名必须',
'email' => '邮箱格式错误',
]; protected $scene = [
'add' => ['name','email'],
'edit' => ['email'],
];
}
 

①静态调用(使用内置的规则验证单个数据,返回值为布尔值

// 日期格式验证
Validate::dateFormat('2016-03-09','Y-m-d'); // true
// 验证是否有效的日期
Validate::is('2016-06-03','date'); // true
// 验证是否有效邮箱地址
Validate::is('thinkphp@qq.com','email'); // true
// 验证是否在某个范围
Validate::in('a',['a','b','c']); // true
// 验证是否大于某个值
Validate::gt(10,8); // true
// 正则验证
Validate::regex(100,'\d+'); // true

②模型验证(在模型中的验证方式

$User = new User;
$result = $User->validate(
[
'name' => 'require|max:25',
'email' => 'email',
],
[
'name.require' => '名称必须',
'name.max' => '名称最多不能超过25个字符',
'email' => '邮箱格式错误',
]
)->save($data);
if(false === $result){
// 验证失败 输出错误信息
dump($User->getError());
}

③控制器验证(控制器中进行验证

如果你需要在控制器中进行验证,并且继承了\think\Controller的话,可以调用控制器类提供的validate方法进行验证,如下:

$result = $this->validate(
[
'name' => 'thinkphp',
'email' => 'thinkphp@qq.com',
],
[
'name' => 'require|max:25',
'email' => 'email',
]);
if(true !== $result){
// 验证失败 输出错误信息
dump($result);
}

控制器中的验证代码可以简化为:

$result = $this->validate($data,'User');
if(true !== $result){
// 验证失败 输出错误信息
dump($result);
}

如果要使用场景,可以使用:

$result = $this->validate($data,'User.edit');
if(true !== $result){
// 验证失败 输出错误信息
dump($result);
}

在validate方法中还支持做一些前置的操作回调,使用方式如下:

$result = $this->validate($data,'User.edit',[],[$this,'some']);
if(true !== $result){
// 验证失败 输出错误信息
dump($result);
}
 

TP5验证规则使用的更多相关文章

  1. TP5验证规则

    系统内置的验证规则如下: 格式验证类 require 验证某个字段必须,例如:'name'=>'require' number 或者 integer 验证某个字段的值是否为数字(采用filter ...

  2. 转载:【TP5.0】TP5 Validate 验证规则

    下面是部分tp5内置的验证规则: 格式验证类: 'name'=>'require' 验证某个字段的值是否为数字(采用filter_var验证),例如: number 或者 integer 'na ...

  3. tp5内置验证规则

    验证规则 描述 require 必须验证 alpha 是否为字母 alphaNum 是否为字母和数字 alphaDash 是否为字母.数字,下划线_及破折号- number 是否为数字 integer ...

  4. TP5 验证-内置规则

    系统内置的验证规则如下: 格式验证类 require 验证某个字段必须,例如: 'name'=>'require' number 或者 integer 验证某个字段的值是否为数字(采用filte ...

  5. Yii1.1的验证规则

    在Yii1.1的数据验证是由CValidator完成,在CValidator中提供了各种基本的验证规则 <?php public static $builtInValidators=array( ...

  6. ThinkPhp5.0模型验证规则

    Tp5提供了模型数据规则的验证功能,用于在数据save或者update前验证数据的有效性.Tp5提供校验规则的类为\Think\Validate,默认提供的校验规则可以查看该文件. 在Model文件中 ...

  7. EF里如何定制实体的验证规则和实现IObjectWithState接口进行验证以及多个实体的同时验证

    之前的Code First系列文章已经演示了如何使用Fluent API和Data Annotation的方式配置实体的属性,比如配置Destination类的Name属性长度不大于50等.本文介绍E ...

  8. [Asp.net MVC]Asp.net MVC5系列——在模型中添加验证规则

    目录 概述 在模型中添加验证规则 自定义验证规则 伙伴类的使用 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5 ...

  9. Thinkphp 1.验证规则 2.静态定义 3.动态验证

    一.验证规则 数据验证可以对表单中的字段进行非法的验证操作.一般提供了两种验证方式: 静态定 义($_validate 属性)和动态验证(validate()方法). //验证规则 array( ar ...

随机推荐

  1. Quartz.net Trigger触发器下 Cron表达式的格式

    有位博主写的不错,样式标准好理解,借鉴下. foamflower 1.   CronTrigger时间格式配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] ...

  2. Best code水题之路

    BestCoder 2nd Anniversary: 1001.Oracle There is once a king and queen, rulers of an unnamed city, wh ...

  3. SpringMVC后台接收list类型的数据的实现方式

    一.背景 最近在做一些东西的时候,遇到一个需要Springmvc后台接收list类型数据的需求,几经辗转才完美解决了这个问题,今天记下来方便以后使用,也分享给需要的小伙伴们~ 二.实现方式 1.实现方 ...

  4. 阿里云centos7基于搭建VPN

    本文参考自:http://www.xxkwz.cn/1495.html 前段时间使用pptp搭建了一个VPN,速度很快,但是用了大概一个月挂了,估计是被墙了吧,于是,用shadowsocks重新搭建了 ...

  5. adb 的使用

    打出log:adb logcat -s fliterName     ps:(fliterName就是你的tag)

  6. xml之dom4j解析

    * 使用dom4j解析xml 实例在java520里面 TextDom4j * dom4j,是一个组织,针对xml解析,提供解析器dom4j * dom4j不是javase的一部分,想要使用第一步需要 ...

  7. Selenium安装失败WebDriverException: Message: 'gechodriver' executable needs to be in PATH

    在搭建Python+Selenium自动化测试时,用python通过WebDriver驱动Firefox浏览器时,一直无法执行测试用例. 报错信息:WebDriverException: Messag ...

  8. 【maven】pom.xml报错:Cannot detect Web Project version.

    新建的maven项目 报错如下: Cannot detect Web Project version. Please specify version of Web Project through &l ...

  9. Coming

    Hi,there, I am coming here to keep a great habit-one day a blog to share what new I have learned tod ...

  10. c语言实现开灯问题

    开灯问题: 有n盏灯,编号为1~n,第1个人把所有灯打开,第2个人按下所有编号为2 的倍数的开关(这些灯将被关掉),第3 个人按下所有编号为3的倍数的开关(其中关掉的灯将被打开,开着的灯将被关闭),依 ...