PHP code example of devlop / laravel-failed-validation-response

1. Go to this page and download the library: Download devlop/laravel-failed-validation-response library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

devlop / laravel-failed-validation-response example snippets


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);
    }
}