Skip to content

Commit 94445c4

Browse files
Change controller validate helper method to behave like Laravel (#1247) (#1248)
* Change controller validate helper method to behave like Laravel (#1247) * Set response on validation exception, which is expected by exception handler. * Remove used import * style conformity * Add backwards compatibility for removed methods: extractInputFromRules and throwValidationException * Cleanup * code style * Update ProvidesConvenienceMethods.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent 057328d commit 94445c4

File tree

1 file changed

+19
-34
lines changed

1 file changed

+19
-34
lines changed

src/Routing/ProvidesConvenienceMethods.php

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Illuminate\Contracts\Bus\Dispatcher;
88
use Illuminate\Http\JsonResponse;
99
use Illuminate\Http\Request;
10-
use Illuminate\Support\Str;
1110
use Illuminate\Validation\ValidationException;
1211
use Illuminate\Validation\Validator;
1312

@@ -64,41 +63,27 @@ public function validate(Request $request, array $rules, array $messages = [], a
6463
{
6564
$validator = $this->getValidationFactory()->make($request->all(), $rules, $messages, $customAttributes);
6665

67-
if ($validator->fails()) {
68-
$this->throwValidationException($request, $validator);
66+
try {
67+
$validated = $validator->validate();
68+
69+
if (method_exists($this, 'extractInputFromRules')) {
70+
// Backwards compatability...
71+
$validated = $this->extractInputFromRules($request, $rules);
72+
}
73+
} catch (ValidationException $exception) {
74+
if (method_exists($this, 'throwValidationException')) {
75+
// Backwards compatability...
76+
$this->throwValidationException($request, $validator);
77+
} else {
78+
$exception->response = $this->buildFailedValidationResponse(
79+
$request, $this->formatValidationErrors($validator)
80+
);
81+
82+
throw $exception;
83+
}
6984
}
7085

71-
return $this->extractInputFromRules($request, $rules);
72-
}
73-
74-
/**
75-
* Get the request input based on the given validation rules.
76-
*
77-
* @param \Illuminate\Http\Request $request
78-
* @param array $rules
79-
* @return array
80-
*/
81-
protected function extractInputFromRules(Request $request, array $rules)
82-
{
83-
return $request->only(collect($rules)->keys()->map(function ($rule) {
84-
return Str::contains($rule, '.') ? explode('.', $rule)[0] : $rule;
85-
})->unique()->toArray());
86-
}
87-
88-
/**
89-
* Throw the failed validation exception.
90-
*
91-
* @param \Illuminate\Http\Request $request
92-
* @param \Illuminate\Contracts\Validation\Validator $validator
93-
* @return void
94-
*
95-
* @throws \Illuminate\Validation\ValidationException
96-
*/
97-
protected function throwValidationException(Request $request, $validator)
98-
{
99-
throw new ValidationException($validator, $this->buildFailedValidationResponse(
100-
$request, $this->formatValidationErrors($validator)
101-
));
86+
return $validated;
10287
}
10388

10489
/**

0 commit comments

Comments
 (0)