Skip to content

Commit

Permalink
Specify a custom HttpResponseException when validation rules fails
Browse files Browse the repository at this point in the history
  • Loading branch information
hschimpf committed Sep 22, 2023
1 parent ef43c3e commit 228d334
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/ResourceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace HDSSolutions\Laravel\API;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\Response;
use RuntimeException;

class ResourceRequest extends FormRequest implements Contracts\ResourceRequest {
Expand Down Expand Up @@ -49,4 +52,11 @@ protected function destroy(): array {
return [];
}

protected function failedValidation(Validator $validator): void {
throw new HttpResponseException(response()->json([
'message' => 'Request validation failed',
'data' => $validator->errors(),
], Response::HTTP_UNPROCESSABLE_ENTITY));
}

}

0 comments on commit 228d334

Please sign in to comment.