简体   繁体   English

Laravel请求验证器重定向并出现错误

[英]Laravel request validator redirect with errors

I would like to make a validator through the usage of Laravel requests, and validation is working fine. 我想通过使用Laravel请求来进行验证,验证工作正常。 But if I don't do them in the controller I can't return back to view with errors if validator fails. 但是,如果我没有在控制器中执行这些操作,并且验证程序失败,我将无法返回查看错误。

Is it possible to implement this: 是否可以实现这一点:

    if ($validator->fails()) {
        return redirect('post/create')
                    ->withErrors($validator)
                    ->withInput();
    }

within the custom request? 在自定义请求中? Something like this maybe: 可能是这样的:

public function rules()
{
    return [
        'name' => 'required',
        'email' => 'required|email|unique:users',
        'password' => 'required|min:6|confirmed',
    ]; // ->withErrors()
}

FormRequest by default returns the user back to the previous page with the errors and input. 默认情况下,FormRequest使用户返回错误和输入内容回到上一页。 You don't have to specify it manually. 您不必手动指定它。 Just set the rules and use the newly created FormRequest in your controller method instead of using the Request object. 只需设置规则并在控制器方法中使用新创建的FormRequest ,而不是使用Request对象。

This is what happens under the hood. 这是在幕后发生的事情。

return $this->redirector->to($this->getRedirectUrl())
            ->withInput($this->except($this->dontFlash))
            ->withErrors($errors, $this->errorBag);

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

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