简体   繁体   English

在 Laravel 7 中验证失败时如何禁用严格重定向?

[英]How do I disable strict redirection when validation fails in Laravel 7?

Documentation : 文档

In the case of a traditional HTTP request, a redirect response will be generated, while a JSON response will be sent for AJAX requests.对于传统的 HTTP 请求,将生成重定向响应,而 AJAX 请求将发送 JSON 响应。

In Laravel 7, when validation fails, it automatically redirects user back to the previous page.在 Laravel 7 中,当验证失败时,它会自动将用户重定向回上一页。 Only when request is AJAX-like, Laravel generates JSON-error.只有当请求类似于 AJAX 时,Laravel 才会生成 JSON 错误。

So my question is how to absolutely disable redirection when validation fails in Laravel 7 regardless request type?所以我的问题是,无论请求类型如何,当 Laravel 7 中的验证失败时,如何绝对禁用重定向?

You need to do a manual validation, instead of using:您需要进行手动验证,而不是使用:

$request->validate()

you need to do it like this:你需要这样做:

$validator = Validator::make($request->all(), [
    'value1' => 'required|max:255',
    'value2' => 'required',
]);

if ($validator->fails())          
    //Do whatever you want
}

//Continue normally if there are no validation errors

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

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