thinkphp

ThinkPHP5.0验证

2017-05-07  本文已影响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',
   ];
}

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

上一篇下一篇

猜你喜欢

热点阅读