1

I'm working on a Laravel 5.4 project and have multiple pages with the same url
e.g. 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);
    } 
}
3
  • Could you please provide the code of the function in your controller that does the redirect? It looks like you could just return a session variable when redirecting to tell your view what the current step is. Commented Jan 15, 2018 at 21:14
  • @MarcBellêtre I added my code under Update #1 Commented Jan 15, 2018 at 21:23
  • 1
    I don't understand what is wrong here. You know you can't "go back" to a POST page. A POST page actually doesn't exist as a page view is always a GET request. So what happens here is correct. If the validation fails you are being redirected back to the form. Could you be more specific about how you ask for the phone number and then the pin number? Maybe by providing more relevant code? Commented Jan 15, 2018 at 21:35

2 Answers 2

2

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);
} 
Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

the init method is called from a GET request. How would I handle the POST request from GET?
Use $request->isMethod("post");
The route could be handled using the match method. Route::match(['get', 'post'], '/order/verify/{encrypted_key}', 'PinVerificationController@init');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.