【问题标题】:Lumen - Validation method redirects to root of the websiteLumen - 验证方法重定向到网站的根目录
【发布时间】:2015-09-10 10:31:26
【问题描述】:

我的问题是我的表单验证失败,但方法 $this->validate($request, $rules, $messages) 不会重定向 -> back() 而是重定向到根页面 /。

验证失败时是否有可能更改重定向?

【问题讨论】:

    标签: laravel-validation lumen laravel-5.1


    【解决方案1】:

    我昨天也遇到了同样的问题,解决办法是制作你自己的验证器实例:

    #be sure to include the Validator class
    use Validator;
    
    #in your function (if input is coming from a form)
    v = Validator::make($request->all(), ['fieldname1', 'fieldname2']);
    if ($v->fails()) {
        #return json error
        return response()->json(['success' => false, 'errors' => $v->errors()]);
        #other return could be
        #return view('page/name', 'errors' => $v->errors());
    }
    // process code here since we've passed validation
    

    【讨论】:

      【解决方案2】:

      在trait ValidatesRequests 中有一个buildFailedValidationResponse 函数,当验证失败时重定向(而且它不是ajax 请求或json 请求)

      在它调用$this->getRedirectUrl()的方法中,因此请确保正确设置此函数访问的任何属性

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-16
        • 1970-01-01
        • 1970-01-01
        • 2015-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多