简体   繁体   English

当Laravel 5.4中的验证失败时,我该如何重定向回去?

[英]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 我正在一个Laravel 5.4项目上,并且有多个具有相同URL的页面
eg www.blahblah.com/order/verify/{encryption_key} 例如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. 但这是路由到GET页面而不是POST页面。

Why is this happening? 为什么会这样呢?
UPDATE #1 更新#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. 但正如我所见,您重定向了back(),它必须来自您的控制器。

My suggestion is you use the formRequest class instead of the validator in your controller. 我的建议是您使用formRequest类而不是控制器中的验证器。

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. 您会看到FormRequest类的getRedirectUrl()方法测试了该类的某些特殊属性,如果找不到任何值,则使用Illuminate \\ Routing \\ UrlGenerator :: previous()返回到重定向。 )生成的网址。 Those properties that the FormRequest checks, are the redirection options you have. FormRequest检查的那些属性是您拥有的重定向选项。

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. 现在,您有两种更改方式,可以通过将属性放在每个表单请求类都继承的抽象类App \\ Http \\ Requests \\ Request中来全局更改所提出的每个表单请求。 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. 但是,如果您坚持在控制器中进行验证,则可以编写if语句。 so that if the validator fails it redirect to a specific path like page 2 path in this situation. 因此,在这种情况下,如果验证程序失败,它将重定向到特定的路径,例如页面2的路径。 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()). 只用相同的方法(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. 用户可以轻松地纠正自己的错误(因为可以填写表格,并且会自动再次显示它),然后再次提交表格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel 5.4:当我有3个帖子页面使用相同的网址时,如何重定向回正确的页面? - Laravel 5.4: How do I redirect back to correct page when I have 3 post pages using the same url? 验证失败后,Laravel 5.1如何重定向回当前页面? - Laravel 5.1 how to redirect back to the current page after validation fails? 在 Laravel 7 中验证失败时如何禁用严格重定向? - How do I disable strict redirection when validation fails in Laravel 7? 如何在没有任何重定向的情况下发回登录错误? Laravel 5.4 - How to send login errors back without any redirect ? Laravel 5.4 如果验证方法因 Laravel 失败,如何重定向回来 - How to redirect back if validate method fails with Laravel Laravel 5.4重定向回预期的URL - Laravel 5.4 Redirect back to intended url Laravel 5.4 验证失败后不会重定向 - Laravel 5.4 does not redirect after unsuccessful validation 在Laravel 5.4中会话超时时如何重定向到路由注销 - How to redirect to route logout when session timeout in laravel 5.4 Laravel 5中请求验证失败后,如何自定义重定向URL? - How to customize redirect url after request validation fails in laravel 5? 对于python文件,Laravel 5.4文件上载验证失败 - Laravel 5.4 file upload validation fails for python files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM