<?php
namespace app\index\controller;
use think\Controller;
use app\validate\Userc;
use app\facade\User;
use think\Facade\Validate;
class Validatetest extends Controller
{
//实例化调用
public function test1()
{
validate = new Userc;
if (!data)) {
return $validate->getError();
}
return '验证通过';
}
//静态代理调用
public function test2()
{
$data = [
'name' => 'lzqlzqlzq',
'email' => '2313@qq.com',
'password' => '3123asdasd',
'mobile' => '13138349148',
];
if (!User::check($data)) {
return User::getError();
}
return '验证通过';
}
//Controller类属性 validate 调用
public function test3()
{
$data = [
'name' => 'lzqlzqlzq',
'email' => '2313@qq.com',
'password' => '3123asdasd',
'mobile' => '13138349148',
];
$res = $this->validate($data, 'app\validate\User');
if (true !== $res) {
return $res;
}
return '验证通过';
}
//通过rule方法调用
public function test4()
{
//2. 独立验证: 使用的是验证器类 think\Validate 中的 rule() 方法
// rule() 方法实际上就是完成给当前类的 protected $rule = [] 初始化
// 独立验证就是不依赖: 不依赖于用户自定义的验证器类: app\validate\User.php
//当前的验证规则
$rule = [
'name|姓名' => [
'require' => 'require',
'min' => 5,
'max' => 20,
],
'email|邮箱' => [
'require' => 'require',
'min' => 6,
'max' => 20,
'email' => 'email',
],
'password|密码' => [
'require' => 'require',
'min' => 6,
'max' => 12,
'alphaNum' => 'alphaNum',
],
'mobile|手机' => [
'require' => 'require',
'mobile' => 'mobile',
],
];
Validate::rule($rule);
//要验证的数据
$data = [
'name' => 'lzqlzqlzq',
'email' => '2313@qqq.com',
'password' => '3123asdasd',
'mobile' => '13138349148',
];
$res = Validate::check($data);
if (true !== $res) {
return Validate::getError();
}
return '验证通过';
}
}