tp5(5.1也能用)系统验证器的直接使用,输出自定义错误信息

<?php
namespace app\index\controller;
use think\Controller;
//不建立验证器的话可以引入系统自带的验证器类,较为简单
use think\Validate;
class Index extends Controller
{
    public function index()
    {   
               echo 6666666; 
              $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'   => 10000,
                        'email' => 'thinkphp@qq.com',
                    ];
                    //实例化系统验证器
                    $validate = new Validate($rule, $msg);
                    //check验证是否正确
          $result   = $validate->check($data);
          //getError返回错误信息
          $error=$validate->getError($data);
          var_dump($error);


 
     }
}
THE END