thinkphp学习-验证器

1、新建独立验证类

namespace app\api\controller\v1;


use think\Validate;

class ValidateTest extends Validate
{
    protected $rule = [
        'name' => 'require|max:5',
        'email' => 'email'
    ];
}

2、验证文件中引用验证类,new对象进行验证

namespace app\api\controller\v1;
use think\Request;
use think\Validate;
use app\api\controller\v1\ValidateTest;

class Banner
{
    public function getBanner($id){
        $data = [
            'name' => 'gaoxigang',
            'email' => 'gaoxigang@163.com'
        ];

        $validate = new ValidateTest();
//        $validate = new Validate([
//            'name' => 'require|max:20',
//            'email' => 'email'
//        ]);
        $result = $validate->batch()->check($data);
        //var_dump($result);

        var_dump($validate->getError());
    }
}

 

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注