今天来讲一下,Lumen的Validator函数

1
2
3
4
5
6
7
8
9
10
11
use Validator;
 
...
 
Class .. {
 
public function ..(){
 
    Validator::make($input$rules$message$attributes)->validate();
 
}
1
use Validator是可以直接引用的,虽然不能直接找到该命名空间的对应的位置。也可以直接在控制器use和使用Validator::make()。
1
至于类名和函数名就随意啦,$input为传入验证的数组,$rule为验证规则,$message为返回的规则,$attributes为验证字段的对应中文注释。废话少说,先模拟一个标准的数据,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$input = [
      'typeid' => 1,
      'title'  =>  '测试标题',
      'content' => '测试内容'
];
 
$rules = [
        'typeid' => 'required|numeric',
        'title' => 'required',
        'content' => 'required'
];
 
$message = [
       "required" => ":attribute 不能为空",
       "numeric" => ":attribute 格式不正确"
];
 
$attributes = [
        'typeid' => '分类id',
        'title' => '标题',
        'content' => '内容'
];

执行后,可能报错Illuminate\Validation\ValidationException: The given data failed to pass validation ...... vendor\illuminate\validation\Validator.php on line305

这是Lumen的异常处理机制,vendor\illuminate\validation\Validator.php

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
   * Run the validator's rules against its data.(运行验证的规则对数据。)
   *
   * @return void
   *
   * @throws \Illuminate\Validation\ValidationException
   */
  public function validate()
  {
      if ($this->fails()) {
          throw new ValidationException($this);
      }
  }

  看看vendor\illuminate\validation\ValidationException.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class ValidationException extends Exception
{
  ...
  /**
     * Create a new exception instance.(创建一个新的异常实例。)
     *
     * @param  \Illuminate\Contracts\Validation\Validator  $validator
     * @param  \Symfony\Component\HttpFoundation\Response  $response
     * @return void
     */
    public function __construct($validator$response = null)
    {
        parent::__construct('The given data failed to pass validation.');
 
        $this->response = $response;
        $this->validator = $validator;
}

 

直接这样抛出异常肯定不ok,接下看看解决方法,

方法一,异常拦截

进入app\Exceptions\Hanlder.php

1
2
3
4
5
6
7
8
9
public function render($request, Exception $e)
{
  //自带数据验证错误返回
  if ($e instanceof \Illuminate\Validation\ValidationException) {
    return $this->handleValidationException($request$e);
  }
 
  return parent::render($request$e);
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//获取自带数据验证错误信息
protected function handleValidationException($request$e)
{
  $errors = @$e->validator->errors()->toArray();
  $message = null;
  if (count($errors)) {
    $firstKey array_keys($errors)[0];
    $message = @$e->validator->errors()->get($firstKey)[0];
    if (strlen($message) == 0) {
      $message "An error has occurred when trying to register";
    }
  }
 
  if ($message == null) {
    $message "An unknown error has occured";
  }
 
  return $message;
}

  

结果会返回第一个验证不过的对应信息。

不过直接在异常这里做处理不太合理,方法二

1
2
3
4
5
6
7
8
//Validator::make($input, $rules , $message, $attributes)->validate();
$validator = Validator::make($input$rules$message$attributes);
$failed $validator->failed();
$messages $validator->messages();
 
if(count($messages) != 0){
      dataReturn(1,current(current($messages))[0]);
}

调用failed和messages方法来处理会优雅许多!

接下来说一下大家关心的手机验证,

1
2
3
Validator::extend('mobile'function($attribute$value$parameters) {
            return preg_match("/^1[34578]{1}\d{9}$/",$value);
        });
1
"font-size: 14px; line-height: 21px; white-space: normal">"font-family: "PingFang SC", "Helvetica Neue", Helvetica, Arial, sans-serif">这段代码应该很熟悉吧,不过到底放在哪里合适呢?我是这么做的

bootstrap/app.phpj加上
1
$app->register(App\Providers\Validate\ValidateServiceProvider::class);
1

然后创建对应服务类app/Providers/Validate/ValidateServiceProvider.php

1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 
namespace App\Providers\Validate;
use Validator;
use Illuminate\Support\ServiceProvider;
 
class ValidateServiceProvider extends ServiceProvider
{
 
    /**
     * 启动应用服务
     *
     * @return void
     */
    public function boot()
    {
 
        Validator::extend('mobile'function($attribute$value$parameters) {
            return preg_match("/^1[34578]{1}\d{9}$/",$value);
        });
      
    }
 
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
         
    }
}

加入手机验证

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$input = [
      'typeid' => 1,
      'title'  =>  '测试标题',
      'content' => '测试内容',
      'phone' => '1881'
];
 
$rules = [
        'typeid' => 'required|numeric',
        'title' => 'required',
        'content' => 'required',
        'phone' =>  'mobile'
];
 
$message = [
       "required" => ":attribute 不能为空",
       "numeric" => ":attribute 格式不正确",
       "mobile" =>  ":attribute 手机格式不正确"
];
 
$attributes = [
        'typeid' => '分类id',
        'title' => '标题',
        'content' => '内容',
        'phone' => '联系方式'
];

就这样简单的实现了手机验证!

如果不想每次都传$message的话,可以看看这里\vendor\laravel\lumen-framework\resources\lang\en\validation.php

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
 
return [
 
    /*
    |--------------------------------------------------------------------------
    | Validation Language Lines
    |--------------------------------------------------------------------------
    |
    | The following language lines contain the default error messages used by
    | the validator class. Some of these rules have multiple versions such
    | as the size rules. Feel free to tweak each of these messages here.
    |
    */
 
    'accepted'             => 'The :attribute must be accepted.',
    'active_url'           => 'The :attribute is not a valid URL.',
    'after'                => 'The :attribute must be a date after :date.',
    'after_or_equal'       => 'The :attribute must be a date after or equal to :date.',
    'alpha'                => 'The :attribute may only contain letters.',
    'alpha_dash'           => 'The :attribute may only contain letters, numbers, and dashes.',
    'alpha_num'            => 'The :attribute may only contain letters and numbers.',
    'array'                => 'The :attribute must be an array.',
    'before'               => 'The :attribute must be a date before :date.',
    'before_or_equal'      => 'The :attribute must be a date before or equal to :date.',
    'between'              => [
        'numeric' => 'The :attribute must be between :min and :max.',
        'file'    => 'The :attribute must be between :min and :max kilobytes.',
        'string'  => 'The :attribute must be between :min and :max characters.',
        'array'   => 'The :attribute must have between :min and :max items.',
    ],
    'boolean'              => 'The :attribute field must be true or false.',
    'confirmed'            => 'The :attribute confirmation does not match.',
    'date'                 => 'The :attribute is not a valid date.',
    'date_format'          => 'The :attribute does not match the format :format.',
    'different'            => 'The :attribute and :other must be different.',
    'digits'               => 'The :attribute must be :digits digits.',
    'digits_between'       => 'The :attribute must be between :min and :max digits.',
    'dimensions'           => 'The :attribute has invalid image dimensions.',
    'distinct'             => 'The :attribute field has a duplicate value.',
    'email'                => 'The :attribute must be a valid email address.',
    'exists'               => 'The selected :attribute is invalid.',
    'file'                 => 'The :attribute must be a file.',
    'filled'               => 'The :attribute field is required.',
    'image'                => 'The :attribute must be an image.',
    'in'                   => 'The selected :attribute is invalid.',
    'in_array'             => 'The :attribute field does not exist in :other.',
    'integer'              => 'The :attribute must be an integer.',
    'ip'                   => 'The :attribute must be a valid IP address.',
    'json'                 => 'The :attribute must be a valid JSON string.',
    'max'                  => [
        'numeric' => 'The :attribute may not be greater than :max.',
        'file'    => 'The :attribute may not be greater than :max kilobytes.',
        'string'  => 'The :attribute may not be greater than :max characters.',
        'array'   => 'The :attribute may not have more than :max items.',
    ],
    'mimes'                => 'The :attribute must be a file of type: :values.',
    'mimetypes'            => 'The :attribute must be a file of type: :values.',
    'min'                  => [
        'numeric' => 'The :attribute must be at least :min.',
        'file'    => 'The :attribute must be at least :min kilobytes.',
        'string'  => 'The :attribute must be at least :min characters.',
        'array'   => 'The :attribute must have at least :min items.',
    ],
    'not_in'               => 'The selected :attribute is invalid.',
    'numeric'              => 'The :attribute must be a number.',
    'present'              => 'The :attribute field must be present.',
    'regex'                => 'The :attribute format is invalid.',
    'required'             => 'The :attribute field is required.',
    'required_if'          => 'The :attribute field is required when :other is :value.',
    'required_unless'      => 'The :attribute field is required unless :other is in :values.',
    'required_with'        => 'The :attribute field is required when :values is present.',
    'required_with_all'    => 'The :attribute field is required when :values is present.',
    'required_without'     => 'The :attribute field is required when :values is not present.',
    'required_without_all' => 'The :attribute field is required when none of :values are present.',
    'same'                 => 'The :attribute and :other must match.',
    'size'                 => [
        'numeric' => 'The :attribute must be :size.',
        'file'    => 'The :attribute must be :size kilobytes.',
        'string'  => 'The :attribute must be :size characters.',
        'array'   => 'The :attribute must contain :size items.',
    ],
    'string'               => 'The :attribute must be a string.',
    'timezone'             => 'The :attribute must be a valid zone.',
    'unique'               => 'The :attribute has already been taken.',
    'uploaded'             => 'The :attribute failed to upload.',
    'url'                  => 'The :attribute format is invalid.',
 
    /*
    |--------------------------------------------------------------------------
    | Custom Validation Language Lines
    |--------------------------------------------------------------------------
    |
    | Here you may specify custom validation messages for attributes using the
    | convention "attribute.rule" to name the lines. This makes it quick to
    | specify a specific custom language line for a given attribute rule.
    |
    */
 
    'custom' => [
        'attribute-name' => [
            'rule-name' => 'custom-message',
        ],
    ],
 
    /*
    |--------------------------------------------------------------------------
    | Custom Validation Attributes
    |--------------------------------------------------------------------------
    |
    | The following language lines are used to swap attribute place-holders
    | with something more reader friendly such as E-Mail Address instead
    | of "email". This simply helps us make messages a little cleaner.
    |
    */
 
    'attributes' => [],
 
];

 

默认都是英文,直接改成下面的中文配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
 
return [
 
/*
|--------------------------------------------------------------------------
| Validation Language Lines
|--------------------------------------------------------------------------
|
| The following language lines contain the default error messages used by
| the validator class. Some of these rules have multiple versions such
| as the size rules. Feel free to tweak each of these messages here.
|
*/
 
'accepted'             => ':attribute必须接受',
'active_url'           => ':attribute必须是一个合法的 URL',
'after'                => ':attribute 必须是 :date 之后的一个日期',
'after_or_equal'       => ':attribute 必须是 :date 之后或相同的一个日期',
'alpha'                => ':attribute只能包含字母',
'alpha_dash'           => ':attribute只能包含字母、数字、中划线或下划线',
'alpha_num'            => ':attribute只能包含字母和数字',
'array'                => ':attribute必须是一个数组',
'before'               => ':attribute 必须是 :date 之前的一个日期',
'before_or_equal'      => ':attribute 必须是 :date 之前或相同的一个日期',
'between'              => [
    'numeric' => ':attribute 必须在 :min 到 :max 之间',
    'file'    => ':attribute 必须在 :min 到 :max KB 之间',
    'string'  => ':attribute 必须在 :min 到 :max 个字符之间',
    'array'   => ':attribute 必须在 :min 到 :max 项之间',
],
'boolean'              => ':attribute 字符必须是 true 或 false',
'confirmed'            => ':attribute 二次确认不匹配',
'date'                 => ':attribute 必须是一个合法的日期',
'date_format'          => ':attribute 与给定的格式 :format 不符合',
'different'            => ':attribute 必须不同于 :other',
'digits'               => ':attribute必须是 :digits 位.',
'digits_between'       => ':attribute 必须在 :min 和 :max 位之间',
'dimensions'           => ':attribute具有无效的图片尺寸',
'distinct'             => ':attribute字段具有重复值',
'email'                => ':attribute必须是一个合法的电子邮件地址',
'exists'               => '选定的 :attribute 是无效的.',
'file'                 => ':attribute必须是一个文件',
'filled'               => ':attribute的字段是必填的',
'image'                => ':attribute必须是 jpeg, png, bmp 或者 gif 格式的图片',
'in'                   => '选定的 :attribute 是无效的',
'in_array'             => ':attribute 字段不存在于 :other',
'integer'              => ':attribute 必须是个整数',
'ip'                   => ':attribute必须是一个合法的 IP 地址。',
'json'                 => ':attribute必须是一个合法的 JSON 字符串',
'max'                  => [
    'numeric' => ':attribute 的最大长度为 :max 位',
    'file'    => ':attribute 的最大为 :max',
    'string'  => ':attribute 的最大长度为 :max 字符',
    'array'   => ':attribute 的最大个数为 :max 个.',
],
'mimes'                => ':attribute 的文件类型必须是 :values',
'min'                  => [
    'numeric' => ':attribute 的最小长度为 :min 位',
    'file'    => ':attribute 大小至少为 :min KB',
    'string'  => ':attribute 的最小长度为 :min 字符',