ellipsesynergie / api-response

Simple package to handle response properly in your API.
MIT License
377 stars 53 forks source link

Handle Form Request Validation #21

Closed jonagoldman closed 8 years ago

jonagoldman commented 8 years ago

I spent a few hours trying to understand why I was hitting endpoint api/foo but was getting response from endpoint api/bar.

Turns out that when using Laravel's Form Request Validation (http://laravel.com/docs/5.1/validation#form-request-validation), if the request is not AJAX or does not contains the 'Accept:application/json' header, it gets redirected to the last endpoint.

I'm using Postman to make the requests so the adding the json header will tell Laravel to return a "JsonResponse" with the validation errors instead of redirecting which is good.

This is not a real package issue but if this package is handling api responses in general it will be really nice to handle form validation responses too (like Dingo API package does). What do you think?

maximebeaudoin commented 8 years ago

This is a good question. I think this is not the responsibility of this package to handle laravel form request validation for two reason.

First, i'm trying to keep this package framework agnostic as much as possible for maintainability purpose.

Second, the responsibility of this package is to handle response, not request. I don't want to create some kind of complete restful api package because some good package already exist for that.

You can extend laravel default FormRequest, like Dingo does, very easily. You can throw some king a exception and catch in in the App\Exceptions\Handler https://laravel.com/docs/5.1/errors#render-method