Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent redirect to homepage after invalid in Laravel

I'm developing a RESTful API with Laravel 5.3, so I'm testing some functions and request with my controllers. One thing I need to do is validate the request that my user sends before add a field in my database, so, I use a custom FormRequest to validate it.

When I tested my API in Postman and send my invalid request, response redirect me to homepage. After reading documentation, I found the following statement

If validation fails, a redirect response will be generated to send the user back to their previous location. The errors will also be flashed to the session so they are available for display. If the request was an AJAX request, a HTTP response with a 422 status code will be returned to the user including a JSON representation of the validation errors.

How can I prevent this? Or there is a AJAX mode in Postman? Any suggestion?

like image 647
Golinmarq Avatar asked Sep 02 '16 16:09

Golinmarq


1 Answers

Also this can be achieved without overriding any function. Laravel is built to support both Json & normal pages. Please change your settings in postman and set Accept to application/json like below enter image description here

Laravel is SMART ;-)

like image 199
notnotundefined Avatar answered Sep 21 '22 02:09

notnotundefined