Thinkphp5验证类的使用

<?php
namespace app\validate;
/**
* 用户验证类User.php
*/
use think\Validate;
class User extends Validate
{
protected $rule = [
'name' => [
'require' => 'require',
'min' => 5,
'max' => 20,
],
'email' => [
'require' => 'require',
'email' => 'email',
],
'pass' => [
'require' => 'require',
'min' => 3,
'max' => 12,
'alphaNum'=> 'alphaNum',
],
'mobile' => [
'require' => 'require',
'mobile' => 'mobile',
'max' => 12,
],
];
}

 

---------------

 

<?php
namespace app\index\controller;
use think\Controller;
use app\validate\User;
//Demo9.php
class Demo9 extends Controller
{

public function test()
{
# 要验证的数据
$data = [
'name' => 'Sam567',
'email' => 'sam@163.com',
'pass' => 'd123456ok',
'mobile' => '18000000000'
];
$validate = new User;
if(!$validate->check($data)){
return $validate->getError();
}
return '验证通过!';

}
}

posted @ 2018-03-21 23:54  SamPHP  阅读(1259)  评论(0编辑  收藏  举报