thinkphp5-验证
作者:互联网
独立验证
<?php
namespace app\index\controller;
use think\Controller;
use think\Validate;
class Index extends Controller
{
public function index()
{
$validate = new Validate([
'name' => 'require|max:10',
'email' => 'email'
]);
$data = [
'name' => '胡勇健',
'email' => '308830232@qq.com'
];
if (!$validate->check($data)) {
dump($validate->getError());
}
}
}
验证器
验证器 application/index/validate/User.php
<?php
namespace app\index\validate;
use think\Validate;
class User extends Validate
{
protected $rule = [
'name' => 'require|max:10',
'email' => 'email',
];
}
控制器
<?php
namespace app\index\controller;
use think\Controller;
use think\Loader;
class Index extends Controller
{
public function index()
{
$data = [
'name' => '胡勇健hhhhhhhhhhhh',
'email' => '308830232@qq.com'
];
$validate = Loader::validate('User');
//或使用助手函数validate
//$validate = validate('User');
if (!$validate->check($data)) {
dump($validate->getError());
}
}
}
内置验证规则
require
number
float
boolean
email
array
date
alpha
alphaNum
alphaDash
url
ip
in
between
max:number
静态调用
// 日期格式验证
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
标签:验证,thinkphp5,email,User,validate,true,Validate 来源: https://www.cnblogs.com/hu308830232/p/15489407.html