简体   繁体   中英

How do I redirect back when validation fails in Laravel 5.4?

I'm working on a Laravel 5.4 project and have multiple pages with the same url
eg www.blahblah.com/order/verify/{encryption_key}

My routs are:

Route::get('/order/verify/{encrypted_key}','PinVerificationController@init');
Route::post('/order/verify/{encrypted_key}','PinVerificationController@pinValidation');

The flow is they land on a page first where they enter their phone number, then they go to a second page where they have to enter a pin code. I validate if the pin code is a number, if it isn't then I redirect back with an error message. But they're being redirected to the first page instead.

If the validation fails i'm routing back. I'm doing

return \Redirect::back()->withInput()->withErrors($validator);

but this is routing to the GET page instead of the POST page.

Why is this happening?
UPDATE #1

    public function init(){

    $country_extensions = appUtils::getCountryExtensionDropdown();

    //TODO
    $country_iso_code = "1-US";
    $parameters = compact( 'country_extensions','country_iso_code' );

    return view('/pages/choose_phone_verify_method',$parameters);

}

private function pinValidation(Request $request){
    $validator = \Validator::make($request->all(), [
        'pin_number' => 'required|numeric'
    ]);

    if ($validator->fails()) {
        return \Redirect::back()->withInput()->withErrors($validator);
    } 
}

I don't know if you make your validation in a controller or in a request. But as I can see you redirect back(), and it must be from your controller.

My suggestion is you use the formRequest class instead of the validator in your controller.

You see, the getRedirectUrl() method of the FormRequest class, tests for some special properties on the class, and if it doesn't find any value, it falls back to a redirect, using the Illuminate\\Routing\\UrlGenerator::previous() generated URL. Those properties that the FormRequest checks, are the redirection options you have.

Now you have two options of changing them, either globally in every form request you make, by putting the property in the abstract class App\\Http\\Requests\\Request that every form request class inherits from. Or, in particular, form classes, by simply putting them in the form class itself.

And these are all the options you have for custom redirections :

protected $redirect; // A simple URL. ex: google.com
protected $redirectRoute; // A route name to redirect to.
protected $redirectAction; // A controller action to redirect to.

But if you insist do the validation in your controller you can write an if statement. so that if the validator fails it redirect to a specific path like page 2 path in this situation. like this code below:

if ($validator->fails()) {
        return redirect('path to page 2')->withInput()->withErrors($validator);
    } 

Or you can redirect to route name:

 if ($validator->fails()) {
    return redirect(route('route name'))->withInput()->withErrors($validator);
} 

Wouldn't it be easier to just handle the post request in the same method (init()). That way you would need to redirect, but just display the errors.

And the user could easily correct his errors (since the form could be filled out, and it's automatically shown again) and submit the form again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM