如何优雅的使用 laravel 的 validator验证方法

本文原创自news.mkq.online
版权声明:本文为原创文章,版权牛站新闻所有
转载请注明http://www.niuzhan.com/Bago/
web 开发过程中经常会需要进行参数验证,laravel 中我们常用 validator 或者 request 这两种方法来进行验证,但是这两种验证都不是很方便进行自定义提示信息,自定义验证规则,所以下面来介绍一种很方便的用法:

新建抽象类
001
<?php
002

003
namespace App\Http\Validators;
004

005
use Validator;
006

007
abstract class AbstractValidator
008
{
009

010
/**
011

  • Validator
    012
  • 013

  • @var \Illuminate\Validation\Factory
    014
    */
    015
    protected $validator;
    016

017
/**
018

  • Validation data key => value array
    019
  • 020

  • @var array
    021
    */
    022
    protected $data = array();
    023

024
/**
025

  • Validation errors
    026
  • 027

  • @var array
    028
    */
    029
    protected $errors = array();
    030

031
/**
032

  • Validation rules
    033
  • 034

  • @var array
    035
    */
    036
    protected $rules = array();
    037

038
/**
039

  • Validation messages
    040
  • 041

  • @var array
    042
    */
    043
    protected $messages = array();
    044

045
/**
046

  • Validation codes
    047
  • 048

  • @var array
    049
    */
    050
    protected $codes = array();
    051

052
public function __construct(array $data)
053
{
054
$this->data = $data;
055
$this->before();
056
$this->validator = Validator::make($this->data, $this->rules, $this->messages);
057
$this->after();
058
}
059

060
/**
061

  • Set data to validate
    062
  • 063

  • @return validator
    064
    */
    065
    public function getValidator()
    066
    {
    067
    return $this->validator;
    068
    }
    069

070
/**
071

  • Set data to validate
    072
  • 073

  • @return $this
    074
    */
    075
    public function with(array $data)
    076
    {
    077
    $this->data = $data;
    078
    $this->before();
    079
    $this->validator = $this->validator->make($this->data, $this->rules, $this->messages);
    080
    $this->after();
    081
    return $this;
    082
    }
    083

084
/**
085

  • Validation passes or fails
    086
  • 087

  • @return boolean
    088
    */
    089
    public function passes()
    090
    {
    091
    if ($this->validator->fails()) {
    092
    $this->errors = $this->validator->messages();
    093

094
return false;
095
}
096

097
return true;
098
}
099

100
/**
101

  • Return errors, if any
    102
  • 103

  • @return array
    104
    */
    105
    public function errors()
    106
    {
    107
    return $this->errors;
    108
    }
    109

110
/**
111

  • Return errors codes, if any
    112
  • 113

  • @return array
    114
    */
    115
    public function getCodes()
    116
    {
    117
    return $this->codes;
    118
    }
    119

120
/**
121

  • getRules
    122
  • 123

  • @return array
    124
    */
    125
    public function getRules()
    126
    {
    127
    return $this->rules;
    128
    }
    129

130
/**
131

  • getData
    132
  • 133

  • @return array
    134
    */
    135
    public function getData()
    136
    {
    137
    return $this->data;
    138
    }
    139

140
/**
141

  • getErrors
    142
  • 143

  • @return array
    144
    */
    145
    public function getErrors()
    146
    {
    147
    return $this->errors;
    148
    }
    149

150
/**
151

  • getMessages
    152
  • 153

  • @return array
    154
    */
    155
    public function getMessages()
    156
    {
    157
    return $this->messages;
    158
    }
    159

160
/**
161

  • setRule
    162
  • 163

  • @param string $key
    164
  • @param string $value
    165
  • 166

  • @return $this
    167
    */
    168
    public function setRule($key, $value)
    169
    {
    170
    $this->rules[$key] = $value;
    171

172
return $this;
173
}
174

175
/**
176

  • emptyRules
    177
  • 178

  • @return $this
    179
    */
    180
    public function emptyRules()
    181
    {
    182
    $this->rules = array();
    183

184
return $this;
185
}
186

187
/**
188

  • sometimes
    189
  • 190

  • @param string $attribute
    191
  • @param string|array $rules
    192
  • @param callable $callback
    193
  • 194

  • @return $this
    195
    */
    196
    public function sometimes($attribute, $rules, callable $callback)
    197
    {
    198
    $this->validator->sometimes($attribute, $rules, $callback);
    199

200
return $this;
201
}
202

203
/**
204

  • resolver
    205
  • 206

  • @param Closure $resolver
    207
  • 208

  • @return $this
    209
    */
    210
    public function resolver(Closure $resolver)
    211
    {
    212
    Validator::resolver($resolver);
    213

214
return $this;
215
}
216

217
/**
218

  • replacer
    219
  • 220

  • @param Closure $resolver
    221
  • 222

  • @return $this
    223
    */
    224
    public function replacer($replace, Closure $resolver)
    225
    {
    226
    Validator::replacer($replace, $resolver);
    227

228
return $this;
229
}
230

231
/**
232

  • extendImplicit
    233
  • 234

  • @param Closure $resolver
    235
  • 236

  • @return $this
    237
    */
    238
    public function extendImplicit($extendImplicit, Closure $resolver)
    239
    {
    240
    Validator::extendImplicit($extendImplicit, $resolver);
    241

242
return $this;
243
}
244

245
/**
246

  • extend
    247
  • 248

  • @param string $rule
    249
  • @param \Closure|string $extension
    250
  • @param string $message
    251
  • 252

  • @return $this
    253
    */
    254
    public function extend($rule, $extension, $message = null)
    255
    {
    256
    Validator::extend($rule, $extension, $message);
    257

258
return $this;
259
}
260

261
/**
262

  • before (extend(),resolver())
    263
  • 264

  • @return $this
    265
    */
    266
    public function before()
    267
    {
    268
    }
    269

270
/**
271

  • after(sometimes())
    272
  • 273

  • @return $this
    274
    */
    275
    public function after()
    276
    {
    277
    }
    278
    }
    新建中间件
    01
    <?php
    02

03
namespace App\Http\Middleware;
04

05
use Closure;
06
use \Illuminate\Http\Request;
07

08
class ValidateAdminMiddleware
09
{
10
/**
11

  • This namespace is applied to the controller routes in your routes file.
    12
  • 13

  • In addition, it is set as the URL generator's root namespace.
    14
  • 15

  • @var string
    16
    */
    17
    protected $namespace = 'App\Http\Validators';
    18

19
/**
20

  • Handle an incoming request.
    21
  • 22

  • @param \Illuminate\Http\Request $request
    23
  • @param \Closure $next
    24
  • 25

  • @return mixed
    26
    */
    27
    public function handle(Request $request, Closure $next, $validator = null)
    28
    {
    29
    if ($request->isMethod('POST')) {
    30
    $type = $request->segment(1);
    31
    if ($validator) {
    32
    $validator = $this->namespace . '\' . studly_case($type) . '\' . studly_case($validator) . 'Validator';
    33
    $validator = new $validator($request->all());
    34

35
if (!$validator->passes()) {
36
if ($request->isAjax()) {
37
return $validator->errors()->first();
38
} else {
39
return redirect()->back()
40
->withErrors($validator->getValidator())
41
->withInput();
42
}
43
}
44
}
45
}
46
return $next($request);
47
}
48
}
新建 TestTestValidator
view sourceprint?
01
<?php
02

03
namespace App\Http\Validators\Admin;
04

05
use App\Http\Validators\AbstractValidator;
06

07
class TestValidator extends AbstractValidator
08
{
09
/**
10

  • Validation rules
    11
  • 12

  • @var Array
    13
    */
    14
    protected $rules = array(
    15
    'name' => ['required', 'test', 'min:1'],
    16
    );
    17

18
/**
19

  • Validation messages
    20
  • 21

  • @var Array
    22
    */
    23
    protected $messages = array(
    24
    'name.required' => '必填',
    25
    'name.min' => '最少1个字符',
    26
    'name.test' => '测试',
    27
    );
    28

29
/**
30

  • 自定义验证规则或者扩展Validator类
    31
    */
    32
    public function before()
    33
    {
    34
    $this->extend('test', function ($attribute, $value, $parameters) {
    35
    return bool;
    36
    });
    37
    }
    38
    }
上一篇:javax validation UnexpectedTypeException HV000030 No valid


下一篇:laravel自定义Validator验证规则