Laravel 5 Invalid custom request - do not redirect
I have a custom Request
Laravel 5 class that handles form inputs (POST). The thing is, I want to use the same request class for the GET method, but instead of redirecting the user back to the original request url (which causes an infinite loop), I want to throw an exception (if the request is invalid) like this possibly?
source to share
In your custom class, Request
you can override the failedValidation method that is defined in the class FormRequest
.
those. put this method in a class Request
:
protected function failedValidation(\Illuminate\Validation\Validator $validator) {
throw new \Exception('Error processing request');
}
Overriding the response () method can also be used to return a preferred response, I personally used it to return errors in JSON form, all it had to do was return a JsonResponse with errors and a response code
public function response(array $errors)
{
return new JsonResponse($errors, 422);
}
source to share
You can add something like this to your request method:
if (Request::isMethod('get'))
{
//Here you can add your custom exception.
}
You can see the documentation for more information on this: http://laravel.com/docs/5.0/requests#other-request-information
source to share