rit-sse / old-api

The unified SSE API.
MIT License
1 stars 5 forks source link

this->validate() returns previous request on failed validation #42

Open kristenmills opened 9 years ago

kristenmills commented 9 years ago

Quote from

If validation passes, your code will keep executing normally. However, if validation fails, an Illuminate\Contracts\Validation\ValidationException will be thrown. This exception is automatically caught and a redirect is generated to the user's previous location. The validation errors are even automatically flashed to the session!

This is not the behavior we want. And I spent 3 hours thinking I had seriously broke something when I kept getting a token back on random POST requests when the issue was that my JSON body was just invalid.

djrenren commented 9 years ago

This is useful in the HTML form POST use case not the API server request.

Kristen is correct.

craigcabrey commented 9 years ago

If you read the README, you'll note that I added a comment on this. This behavior occurs only from a browser context. From an AJAX/XmlHtttpRequest context, errors are simply dumped out in a JSON object.

djrenren commented 9 years ago

Seeing as we NEVER want the browser context case can't we just disable this behavior altogether?

craigcabrey commented 9 years ago

So what would happen if we had a <form method="POST" action="/api/v1/members"> with invalid data? Just dump the user at a screen with JSON output?

There's really no reason to spend time or effort on disabling a "feature" (I'd argue it's basic functionality) that we will never even touch. Let's focus our efforts elsewhere.

$.02 deposited.

djrenren commented 9 years ago

@craigcabrey at least it's less mystifying than silently erroring and would indicate that we need to do something about it. Seeing as we're not hosting the front-end on the api server the redirect is actually misbehaving. If we want to redirect with errors in the GET request I'm okay with that but we shouldn't leave it the way it is because it promotes improper usage.

Looks like the solution is to override the response function in your FormRequest subclass: https://github.com/laravel/framework/blob/master/src/Illuminate/Foundation/Http/FormRequest.php