devlop / laravel-failed-validation-response
Trait for Laravel FormRequest to enable the use of custom responses for failed validation attempts.
1.0.2
2024-01-12 05:46 UTC
Requires
- php: ^7.4|^8.0
- laravel/framework: ^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- phpunit/phpunit: ^9.5
- symfony/var-dumper: ^5.2
This package is auto-updated.
Last update: 2024-10-12 07:30:16 UTC
README
Laravel Failed Validation Response
Trait for Laravel FormRequests to enable the use of custom responses for failed validation (or authorization) attempts.
Installation
composer require devlop/laravel-failed-validation-response
Usage
use Devlop\Laravel\Validation\FailedValidationResponse; use Illuminate\Contracts\Validation\Validator; use Illuminate\Foundation\Http\FormRequest; use Symfony\Component\HttpFoundation\Response; class DemoRequest extends FormRequest { use FailedValidationResponse; // ... Your normal FormRequest logic. /** * Get the response for a failed validation attempt. * * @param Validator $validator * @return Response|null */ public function failedValidationResponse(Validator $validator) : ?Response { // Implement this method to use a custom response on validation failure. // Do not implement this method to instead use the default behaviour. // Example: return redirect()->to('/'); } /** * Get the response for a failed authorization attempt. * * @param Validator $validator * @return Response|null */ public function failedAuthorizationResponse(Validator $validator) : ?Response { // Implement this method to use a custom response on authorization failure. // Do not implement this method to instead use the default behaviour. // Example: return response()->json([ 'reason' => 'you are not allowed to that!', ], 401); } }