美文网首页thinkphp
ThinkPHP5.0验证

ThinkPHP5.0验证

作者: 陈智涛 | 来源:发表于2017-05-07 17:05 被阅读0次

一、分类:
1、独立验证
2、验证器
二、代码描述

1.独立验证

        $data1 = [
            'name'=>'chenzhitao',
            'email'=>'zhitaochan@163.com'
        ];
        $validate = new Validate([
            'name'=>'require|max:10',
            'email'=>'email',
        ]);
        //批量验证
        $result = $validate->batch()->check($data1);
//        var_dump( $validate->getError());

2.验证器

        $data2 = [
            'name'=>'chenzhitaoww',
            'email'=>'zhitaochan163.com'
        ];
        $validate2 = new TestValidate($data2);
        $result = $validate2->batch()->check($data1);
        var_dump( $validate2->getError());
//附验证器TestValidate类
<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2017/5/7
 * Time: 下午4:33
 */

namespace app\api\validate;

use think\Validate;
class TestValidate extends Validate
{
   protected $rules = [
       'name'=>'require|max:10',
       'email'=>'email',
   ];
}

三、比较
验证器比独立验证封装性更好

相关文章

网友评论

    本文标题:ThinkPHP5.0验证

    本文链接:https://www.haomeiwen.com/subject/pmautxtx.html