Laravel 5.5 API Form Request Validation Errors
Using Form Requests is one of the ways we ensure our business logic is not bloated. But there are default behaviors you do not want to accommodate while developing an API using Laravel.
Even when we modify our global Exception handler for JSON responses similar to :
We still don’t get respective error according to the failed validations. We can try to create an extension of the Form Requests class so that we can handle the validation errors using DRY principle like so:
That is it! Now you can simply extend your FormRequest class, create your rules and define your authorization. So you can get your error similar to this:
Note : In previous versions of Laravel you need override the response() method instead of the failedValidation() method. Read more here.
If you liked this, clap so other people will see this here on Medium. Also, if you have any questions or observations, use the comment section to share your thoughts.