Closed everton15m closed 7 years ago
Sounds like you might have session issues.
Hi,
My guess is that you are using this boilerplate with Laravel 5.4 in which the method FormRequest::forbiddenResponse
is no longer available in the API.
Therefore, the method override in the abstract class App\Http\Requests\Request
won't get execute to the point that it hits return redirect()->back()->withErrors($this->error);
.
According to Laravel 5.4 documentation below, I think the errors that you are looking for will be flashed to the session.
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.
I would appreciate if someone can validate my assumption.
Yes I am using laravel 5.4 . I will make a video but the error is very inconsistent
This PR might fix that specific error: https://github.com/rappasoft/laravel-5-boilerplate/pull/745
@rappasoft
Thank for the great work of this boilerplate. I think that PR is a good fix. In fact, I did just like that in my local repo but with a slightly different behavior.
I like how you implemented the abstract class of FormRequest in which you redirect back with errors but without the input. However, the method failedAuthorization
in that PR will throws an instance of GeneralException
which means that the App\Exceptions\Handler
will catch it and redirect back a flash error message as well as the input. (See code here).
IMHO, I would choose redirect it without the input as a default.
Cheers!
Why wouldn't you want the input if there was an error on a form?
Actually you're right, i'm probably going to change it back from an exception to a redirect.
@rappasoft
Well, I think the old input from redirection should be optional, not mandatory.
Cheers!
I think more people than not would want it, besides everyones is free to remove whatever they want.
After the updation of laravel documentation, you don't need to override the response() anymore, all you have to do is, just write your bussiness logics inside the protected failedValidation() inside your custom FormRequest class like follows,
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Contracts\Validation\Validator;
/**
* [failedValidation [Overriding the event validator for custom error response]]
* @param Validator $validator [description]
* @return [object][object of various validation errors]
*/
public function failedValidation(Validator $validator) {
//write your bussiness logic here otherwise it will give same old JSON response
throw new HttpResponseException(response()->json($validator->errors(), 422));
}
Many times when validation fails in requests , it just goes back without any errors or previous form input. Has anyone had this problem?