码迷,mamicode.com
首页 > Web开发 > 详细

thinkphp之验证

时间:2019-12-03 14:25:35      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:namespace   出错   his   ida   color   check   return   创建   自定义   

1 验证器、

    1.1创建验证器  

php think make:validate index/User

 

   

namespace app\index\validate;

use think\Validate;

class User extends Validate
{
    protected $rule =   [
        ‘name‘  => ‘require|max:25‘,
        ‘age‘   => ‘number|between:1,120‘,
        ‘email‘ => ‘email‘,    
    ];
    
    protected $message  =   [
        ‘name.require‘ => ‘名称必须‘,
        ‘name.max‘     => ‘名称最多不能超过25个字符‘,
        ‘age.number‘   => ‘年龄必须是数字‘,
        ‘age.between‘  => ‘年龄只能在1-120之间‘,
        ‘email‘        => ‘邮箱格式错误‘,    
    ];
    
}
事实上控制器类提供了一个validate方法可以更方便的进行验证,如下:  推荐使用

namespace app\index\controller;

use think\Controller;

class Index extends Controller
{
    public function index()
    {
        $result = $this->validate(
            [
                ‘name‘  => ‘thinkphp‘,
                ‘email‘ => ‘thinkphp@qq.com‘,
            ],
            ‘app\index\validate\User‘);

        if (true !== $result) {
            // 验证失败 输出错误信息
            dump($result);
        }
    }
}

 

 1.2自定义验证规则

 

自定义验证规则
系统内置了一些常用的规则(参考后面的内置规则),如果不能满足需求,可以在验证器重添加额外的验证方法,例如:

namespace app\index\validate;

use think\Validate;

class User extends Validate
{
    protected $rule = [
        ‘name‘  =>  ‘checkName:thinkphp‘,
        ‘email‘ =>  ‘email‘,
    ];
    
    protected $message = [
        ‘name‘  =>  ‘用户名必须‘,
        ‘email‘ =>  ‘邮箱格式错误‘,
    ];
    
    // 自定义验证规则
    protected function checkName($value,$rule,$data=[])
    {
        return $rule == $value ? true : ‘名称错误‘;
    }
}
验证方法可以传入的参数共有5个(后面三个根据情况选用),依次为:

验证数据  $value
验证规则  $rule   这两个字段常用
全部数据(数组)
字段名
字段描述

 

  独立验证器

 

$rule = [
    ‘name‘  => ‘require|max:25‘,
    ‘age‘   => ‘number|between:1,120‘,
    ‘email‘ => ‘email‘,
];

$msg = [
    ‘name.require‘ => ‘名称必须‘,
    ‘name.max‘     => ‘名称最多不能超过25个字符‘,
    ‘age.number‘   => ‘年龄必须是数字‘,
    ‘age.between‘  => ‘年龄只能在1-120之间‘,
    ‘email‘        => ‘邮箱格式错误‘,
];

$data = [
    ‘name‘  => ‘thinkphp‘,
    ‘age‘   => 10,
    ‘email‘ => ‘thinkphp@qq.com‘,
];

$validate   = Validate::make($rule,$msg);
$result = $validate->check($data);

if(!$result) {
    dump($validate->getError());
}

 

自定义验证规则

 Validate::extend(‘checkTitle‘,function ($value,$rule){
            return strstr($value,$rule)? ‘字符中不能出现:‘.$rule : true;
        });

        $rule = [‘title‘ => ‘require|min:2|checkTitle:张三‘, ‘content‘ => ‘require|min:10‘];

        $msg      = [‘title.require‘   => ‘标题必须‘,
                     ‘content.require‘ => ‘内容必须‘,
                     ‘title.min‘       => ‘标题长度最小为2个字符‘,
                     ‘content.min‘     => ‘内容长度最小为10个字符‘,

        ];

        $validate = Validate::make($rule, $msg);
        $ret      = $validate->check($request->post());

 

thinkphp之验证

标签:namespace   出错   his   ida   color   check   return   创建   自定义   

原文地址:https://www.cnblogs.com/mofei12138/p/11976346.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!