1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Milkmeowo\Framework\Base\Validation; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Http\JsonResponse; |
7
|
|
|
use Illuminate\Routing\UrlGenerator; |
8
|
|
|
use Dingo\Api\Exception\ResourceException; |
9
|
|
|
use Illuminate\Contracts\Validation\Factory; |
10
|
|
|
use Illuminate\Contracts\Validation\Validator; |
11
|
|
|
use Illuminate\Validation\ValidationException; |
12
|
|
|
|
13
|
|
|
trait ValidatesRequests |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The default error bag. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $validatesRequestErrorBag; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Run the validation routine against the given validator. |
24
|
|
|
* |
25
|
|
|
* @param \Illuminate\Contracts\Validation\Validator|array $validator |
26
|
|
|
* @param \Illuminate\Http\Request|null $request |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
|
|
public function validateWith($validator, Request $request = null) |
30
|
|
|
{ |
31
|
|
|
$request = $request ?: app('request'); |
32
|
|
|
|
33
|
|
|
if (is_array($validator)) { |
34
|
|
|
$validator = $this->getValidationFactory()->make($request->all(), $validator); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ($validator->fails()) { |
38
|
|
|
$this->throwValidationException($request, $validator); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Validate the given request with the given rules. |
44
|
|
|
* |
45
|
|
|
* @param \Illuminate\Http\Request $request |
46
|
|
|
* @param array $rules |
47
|
|
|
* @param array $messages |
48
|
|
|
* @param array $customAttributes |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function validate(Request $request, array $rules, array $messages = [], array $customAttributes = []) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$validator = $this->getValidationFactory()->make($request->all(), $rules, $messages, $customAttributes); |
54
|
|
|
|
55
|
|
|
if ($validator->fails()) { |
56
|
|
|
$this->throwValidationException($request, $validator); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Validate the given data with the given rules. |
62
|
|
|
* |
63
|
|
|
* @param \Illuminate\Http\Request $request |
64
|
|
|
* @param array $data |
65
|
|
|
* @param array $rules |
66
|
|
|
* @param array $messages |
67
|
|
|
* @param array $customAttributes |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
public function validateData(Request $request, array $data, array $rules, array $messages = [], array $customAttributes = []) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$validator = $this->getValidationFactory()->make($data, $rules, $messages, $customAttributes); |
72
|
|
|
|
73
|
|
|
if ($validator->fails()) { |
74
|
|
|
$this->throwValidationException($request, $validator); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Validate the given request with the given rules. |
80
|
|
|
* |
81
|
|
|
* @param string $errorBag |
82
|
|
|
* @param \Illuminate\Http\Request $request |
83
|
|
|
* @param array $rules |
84
|
|
|
* @param array $messages |
85
|
|
|
* @param array $customAttributes |
86
|
|
|
* @return void |
87
|
|
|
* |
88
|
|
|
* @throws \Illuminate\Validation\ValidationException |
89
|
|
|
*/ |
90
|
|
|
public function validateWithBag($errorBag, Request $request, array $rules, array $messages = [], array $customAttributes = []) |
91
|
|
|
{ |
92
|
|
|
$this->withErrorBag($errorBag, function () use ($request, $rules, $messages, $customAttributes) { |
93
|
|
|
$this->validate($request, $rules, $messages, $customAttributes); |
94
|
|
|
}); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Throw the failed validation exception. |
99
|
|
|
* |
100
|
|
|
* @param \Illuminate\Http\Request $request |
101
|
|
|
* @param \Illuminate\Contracts\Validation\Validator $validator |
102
|
|
|
* @return void |
103
|
|
|
* |
104
|
|
|
* @throws \Illuminate\Validation\ValidationException |
105
|
|
|
*/ |
106
|
|
|
protected function throwValidationException(Request $request, $validator) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
throw new ResourceException($validator->errors()->first(), $validator->errors()); |
109
|
|
|
// throw new ValidationException($validator, $this->buildFailedValidationResponse( |
|
|
|
|
110
|
|
|
// $request, $this->formatValidationErrors($validator) |
111
|
|
|
// )); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Create the response for when a request fails validation. |
116
|
|
|
* |
117
|
|
|
* @param \Illuminate\Http\Request $request |
118
|
|
|
* @param array $errors |
119
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
120
|
|
|
*/ |
121
|
|
|
protected function buildFailedValidationResponse(Request $request, array $errors) |
122
|
|
|
{ |
123
|
|
|
if ($request->expectsJson()) { |
124
|
|
|
return new JsonResponse($errors, 422); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return redirect()->to($this->getRedirectUrl()) |
128
|
|
|
->withInput($request->input()) |
|
|
|
|
129
|
|
|
->withErrors($errors, $this->errorBag()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Format the validation errors to be returned. |
134
|
|
|
* |
135
|
|
|
* @param \Illuminate\Contracts\Validation\Validator $validator |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
protected function formatValidationErrors(Validator $validator) |
139
|
|
|
{ |
140
|
|
|
return $validator->errors()->getMessages(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get the URL we should redirect to. |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
protected function getRedirectUrl() |
149
|
|
|
{ |
150
|
|
|
return app(UrlGenerator::class)->previous(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get a validation factory instance. |
155
|
|
|
* |
156
|
|
|
* @return \Illuminate\Contracts\Validation\Factory |
157
|
|
|
*/ |
158
|
|
|
protected function getValidationFactory() |
159
|
|
|
{ |
160
|
|
|
return app(Factory::class); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Execute a Closure within with a given error bag set as the default bag. |
165
|
|
|
* |
166
|
|
|
* @param string $errorBag |
167
|
|
|
* @param callable $callback |
168
|
|
|
* @return void |
169
|
|
|
*/ |
170
|
|
|
protected function withErrorBag($errorBag, callable $callback) |
171
|
|
|
{ |
172
|
|
|
$this->validatesRequestErrorBag = $errorBag; |
173
|
|
|
|
174
|
|
|
call_user_func($callback); |
175
|
|
|
|
176
|
|
|
$this->validatesRequestErrorBag = null; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get the key to be used for the view error bag. |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
protected function errorBag() |
185
|
|
|
{ |
186
|
|
|
return $this->validatesRequestErrorBag ?: 'default'; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.