rappasoft / laravel-boilerplate

The Laravel Boilerplate Project - https://laravel-boilerplate.com
https://rappasoft.com
5.59k stars 1.58k forks source link

Validation error goes back without error messages and form input #737

Closed everton15m closed 7 years ago

everton15m commented 7 years ago

Many times when validation fails in requests , it just goes back without any errors or previous form input. Has anyone had this problem?

rappasoft commented 7 years ago

Sounds like you might have session issues.

pairote commented 7 years ago

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.

everton15m commented 7 years ago

Yes I am using laravel 5.4 . I will make a video but the error is very inconsistent

rappasoft commented 7 years ago

This PR might fix that specific error: https://github.com/rappasoft/laravel-5-boilerplate/pull/745

pairote commented 7 years ago

@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!

rappasoft commented 7 years ago

Why wouldn't you want the input if there was an error on a form?

rappasoft commented 7 years ago

Actually you're right, i'm probably going to change it back from an exception to a redirect.

pairote commented 7 years ago

@rappasoft

Well, I think the old input from redirection should be optional, not mandatory.

Cheers!

rappasoft commented 7 years ago

I think more people than not would want it, besides everyones is free to remove whatever they want.

shahrukh4 commented 6 years ago

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)); 
    }