gpt4 book ai didi

php - laravel 5 在哪里处理 ValidationException?

转载 作者:行者123 更新时间:2023-12-03 03:54:57 26 4
gpt4 key购买 nike

If the incoming request was an AJAX request, no redirect will be generated. Instead, an HTTP response with a 422 status code will be returned to the browser containing a JSON representation of the validation errors.

这不起作用!我试图通过 ajax 请求访问该路由,它会重定向回来。

If validation passes, your code will keep executing normally. However, if validation fails, an Illuminate\Contracts\Validation\ValidationException will be thrown. This exception is automatically caught and a redirect is generated to the user's previous location. The validation errors are even automatically flashed to the session!

现在我想知道laravel在哪里捕获这个异常以便我可以修改它?

最佳答案

这是在 FormRequest 类内部处理的:

protected function failedValidation(Validator $validator)
{
throw new HttpResponseException($this->response(
$this->formatErrors($validator)
));
}

您可以在自己的 Request 对象中重写此函数,并以您喜欢的任何方式处理失败的验证。

关于php - laravel 5 在哪里处理 ValidationException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28917830/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com