laravel 自定义验证 Validator::extend

时间:2021-02-17 23:16:13

laravel 自定义验证

 1 $messages = [
 2     'name.integer' => '名字不能为整型', 
 3     'name.max' => '长度不能超过5',
 4 ];
 5 
 6 public static $rules=[
 7     'name' => 'required|integer|max:5|foo_fei', //foo_fei 自定义验证
 8     'score' => 'required|integer',
 9 ];
10 
11 Validator::extend('foo_fei', function ($attribute, $value, $parameters) { //exit('进来了');
12     return $value=='foo';
13     //这里的错误消息在 resources/lang/en/validation.php 
14     // 'custom'               => [
15     //     'attribute-name' => [
16     //         'rule-name' => 'custom-message',
17     //     ],
18     //     'name'=> [
19     //         'foo_fei'=>'你是傻逼'
20     //     ],
21     // ],
22 });
23 
24  $validatedData = Validator::make($data,Student::$rules,$messages);