-
Notifications
You must be signed in to change notification settings - Fork 0
/
HelperValidatesRequest.php
212 lines (192 loc) · 6.42 KB
/
HelperValidatesRequest.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
namespace Illuminate\Foundation\Validation;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Contracts\Validation\Factory;
use Illuminate\Contracts\Validation\Validator;
trait HelperValidatesRequests
{
/**
* The default error bag.
*
* @var string
*/
protected $validatesRequestErrorBag;
/**
* Run the validation routine against the given validator.
*
* @param \Illuminate\Contracts\Validation\Validator|array $validator
* @param \Illuminate\Http\Request|null $request
* @return void
*/
public function validateWith($validator, Request $request = null)
{
$request = $request ?: app('request');
if (is_array($validator)) {
$validator = $this->getValidationFactory()->make($request->all(), $validator);
}
if ($validator->fails()) {
$this->throwValidationException($request, $validator);
}
}
/**
* Validate the given request with the given rules.
*
* @param \Illuminate\Http\Request $request
* @param array $rules
* @param array $messages
* @param array $customAttributes
* @return void
*/
public function validate(Request $request, array $rules, array $messages = [], array $customAttributes = [])
{
if (count($customAttributes) == 0)
{
foreach ($rules as $key => $value)
{
$customAttributes[$key] = trans('lang.'.$key);
}
}
if (isset($rules['translate']) && is_array($rules['translate']))
{
foreach($rules['translate'] as $k => $v)
{
if (str_contains($k, '.'))
{
if (explode('.', $k)[1] == '*')
{
foreach (\App\Lang::all() as $lang)
{
$key = explode('.', $k)[0];
$rules[$key.$lang->id] = $v;
$customAttributes[$key.$lang->id] = trans('lang.'.$key).' ( '.$lang->name.' ) ';
}
}else
{
$lang = \App\Lang::where('code',explode('.', $k)[1]);
if ($lang->exists())
{
$lang = $lang->first();
$key = explode('.', $k)[0];
$rules[$key.$lang->id] = $v;
$customAttributes[$key.$lang->id] = trans('lang.'.$key).' ( '.$lang->name.' ) ';
}
}
}else
{
foreach (\App\Lang::all() as $lang)
{
$key = $k;
$rules[$key.$lang->id] = $v;
$customAttributes[$key.$lang->id] = trans('lang.'.$key).' ( '.$lang->name.' ) ';
}
}
}
}
$rules = array_except($rules, 'translate');
$validator = $this->getValidationFactory()->make($request->all(), $rules, $messages, $customAttributes);
if ($validator->fails()) {
$this->throwValidationException($request, $validator);
}
}
/**
* Validate the given request with the given rules.
*
* @param string $errorBag
* @param \Illuminate\Http\Request $request
* @param array $rules
* @param array $messages
* @param array $customAttributes
* @return void
*
* @throws \Illuminate\Foundation\Validation\ValidationException
*/
public function validateWithBag($errorBag, Request $request, array $rules, array $messages = [], array $customAttributes = [])
{
$this->withErrorBag($errorBag, function () use ($request, $rules, $messages, $customAttributes) {
$this->validate($request, $rules, $messages, $customAttributes);
});
}
/**
* Throw the failed validation exception.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Contracts\Validation\Validator $validator
* @return void
*
* @throws \Illuminate\Foundation\Validation\ValidationException
*/
protected function throwValidationException(Request $request, $validator)
{
throw new ValidationException($validator, $this->buildFailedValidationResponse(
$request, $this->formatValidationErrors($validator)
));
}
/**
* Create the response for when a request fails validation.
*
* @param \Illuminate\Http\Request $request
* @param array $errors
* @return \Illuminate\Http\Response
*/
protected function buildFailedValidationResponse(Request $request, array $errors)
{
if (($request->ajax() && ! $request->pjax()) || $request->wantsJson()) {
return new JsonResponse($errors, 422);
}
return redirect()->to($this->getRedirectUrl())
->withInput($request->input())
->withErrors($errors, $this->errorBag());
}
/**
* Format the validation errors to be returned.
*
* @param \Illuminate\Contracts\Validation\Validator $validator
* @return array
*/
protected function formatValidationErrors(Validator $validator)
{
return $validator->errors()->getMessages();
}
/**
* Get the URL we should redirect to.
*
* @return string
*/
protected function getRedirectUrl()
{
return app(UrlGenerator::class)->previous();
}
/**
* Get a validation factory instance.
*
* @return \Illuminate\Contracts\Validation\Factory
*/
protected function getValidationFactory()
{
return app(Factory::class);
}
/**
* Execute a Closure within with a given error bag set as the default bag.
*
* @param string $errorBag
* @param callable $callback
* @return void
*/
protected function withErrorBag($errorBag, callable $callback)
{
$this->validatesRequestErrorBag = $errorBag;
call_user_func($callback);
$this->validatesRequestErrorBag = null;
}
/**
* Get the key to be used for the view error bag.
*
* @return string
*/
protected function errorBag()
{
return $this->validatesRequestErrorBag ?: 'default';
}
}