验证器处理数据

Original 2019-03-27 10:20:54 265
abstract:<?php namespace app\index\controller; use think\Controller; use app\validate\Staff;//导入验证器规则 use think\Validate;//导入验证器类 class Verity extends Controller {  &
<?php
namespace app\index\controller;
use think\Controller;
use app\validate\Staff;//导入验证器规则
use think\Validate;//导入验证器类
class Verity extends Controller
{
    //验证器  自定义验证器
    public function  demo1()
    {
        //准备一个要验证的数据
        $data = [
            'name'=>'sdsds',
            'sex'=>45,
            'age'=>19
        ];
        $validate = new Staff();
        if (!$validate->check($data)){
            dump($validate->getError());
        }else{
            return '验证通过';
        }
    }
    //简化验证器 $this->validate($data,$rule,$message);
    public function demo2()
    {
        $data = [
            'name'=>'sdsds',
            'sex'=>1,
            'age'=>10
        ];
        //验证规则
        $rule = 'app\validate\Staff';
        $res = $this->validate($data,$rule);
        if(true!==$res){
            return $res;
        }
        return '验证成功';
    }
    //独立验证  实例化验证器类
    public function demo3()
    {
        //validate::make()创建验证规则并返回验证对象
        //$validate->check($data)验证
        //1.创建验证规则
        $rule = ['age'=>'require|between:10,20'];
        //创建错误信息
        $mess = [
            'age.between'=>'年龄必须在10到20之间',
            'age.require'=>'年龄不能为空'
        ];
        //创建数据
        $data = ['age'=>10];
        $validate = Validate::make($rule,$mess);
        $res = $validate->check($data);
        return $res ? '验证通过': $validate->getError();
    }
}


Correcting teacher:天蓬老师Correction time:2019-03-27 10:49:53
Teacher's summary:你是四期的学员吗? 这个作业 , 应该提交到博客中, 而不是这里

Release Notes

Popular Entries