https://github.com/devlop/laravel-failed-validation-response
Trait for Laravel FormRequest to enable the use of custom responses for failed validation attempts.
https://github.com/devlop/laravel-failed-validation-response
form-request laravel php trait validation
Last synced: 16 days ago
JSON representation
Trait for Laravel FormRequest to enable the use of custom responses for failed validation attempts.
- Host: GitHub
- URL: https://github.com/devlop/laravel-failed-validation-response
- Owner: devlop
- License: mit
- Created: 2021-03-12T09:55:37.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-01-12T05:47:09.000Z (over 2 years ago)
- Last Synced: 2024-04-25T19:21:09.437Z (about 2 years ago)
- Topics: form-request, laravel, php, trait, validation
- Language: PHP
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Laravel Failed Validation Response
Trait for [Laravel FormRequests](https://laravel.com/docs/8.x/validation#form-request-validation)
to enable the use of custom responses for failed validation (or authorization) attempts.
# Installation
```bash
composer require devlop/laravel-failed-validation-response
```
# Usage
```php
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);
}
}
```