laravel FormRequest 验证失败返回 500 错误而不是 422 错误(5.2 升级后)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34636445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:58:58  来源:igfitidea点击:

FormRequest failed validation returns 500 error instead of 422 with errors (after 5.2 upgrade)

laravellaravel-5.2

提问by FooBar

After updating from L5.1 to L5.2, I no longer receive a JSON object as response on a failed FormRequest (i.e. on an AJAX post request).

从 L5.1 更新到 L5.2 后,我不再收到 JSON 对象作为对失败的 FormRequest(即 AJAX 发布请求)的响应。

Usually I would receive a 422 response like:

通常我会收到 422 响应,例如:

[
    email: 'E-mail is invalid',
    firstname: 'Firstname must be at least 2 characters'
]

But now I receive a 500 error page:

但现在我收到一个 500 错误页面:

500 response page

500响应页面

I have ensured that my AJAX calls have application/jsonas Acceptheader.

我已确保我的AJAX调用都application/json作为Accept标题。

Update

更新

And no, I am not manually catching this exception. I am using the default FormRequest that Laravel provides. As they state in documentation: When using the validate method during an AJAX request, Laravel will not generate a redirect response. Instead, Laravel generates a JSON response containing all of the validation errors. This JSON response will be sent with a 422 HTTP status code.

不,我没有手动捕获此异常。我正在使用 Laravel 提供的默认 FormRequest。正如他们在文档中所述:在 AJAX 请求期间使用 validate 方法时,Laravel 不会生成重定向响应。相反,Laravel 会生成一个包含所有验证错误的 JSON 响应。此 JSON 响应将与 422 HTTP 状态代码一起发送。

Like so: php artisan make:request StoreBlogPostRequest(https://laravel.com/docs/5.1/validation#form-request-validation)

像这样:php artisan make:request StoreBlogPostRequesthttps://laravel.com/docs/5.1/validation#form-request-validation

采纳答案by Cowwando

@Mattias!

@马蒂亚斯!

I've recently had the same issue and I wasted more than 2 hours trying to understand what actually causes this problem. Disabling debugging in .env file causes form validation to display 500 since the FormValidator throws ValidationException (and it is unhandled). The solution for this issue was: Open app\Exceptions\Handler.php

我最近遇到了同样的问题,我浪费了 2 个多小时试图了解导致此问题的实际原因。在 .env 文件中禁用调试会导致表单验证显示 500,因为 FormValidator 抛出 ValidationException(并且它是未处理的)。此问题的解决方案是:打开app\Exceptions\Handler.php

private function handleExceptions($e)
    {
       // Add anywhere in this method the following code
       // It does what the FormValidator does.

        if($e instanceof ValidationException) {

            return redirect()->back()->withErrors($e->validator->getMessageBag()->toArray());
        }

        return response()->view('errors.500', [], 500);
    }

回答by Drew

To reiterate what the others have said, you more than likely have a modified app/Exceptions/Handler.phpin your project where you have some code that is preventing you from seeing the result you'd hoped for.

重申其他人所说的话,您很可能app/Exceptions/Handler.php在您的项目中进行了修改,其中您有一些代码阻止您看到您希望的结果。

All exceptions are handled by the App\Exceptions\Handler class. This class contains two methods: report and render.

所有异常都由 App\Exceptions\Handler 类处理。这个类包含两个方法:report 和render。

Double check app/Exceptions/Handler.phpand the documentation regarding exception handlers at https://laravel.com/docs/5.2/errors#the-exception-handlerto make sure you're handling exceptions as you'd intended.

app/Exceptions/Handler.phphttps://laravel.com/docs/5.2/errors#the-exception-handler仔细检查和有关异常处理程序的文档,以确保您按预期处理异常。

回答by christostsang

I came against the same problem using Laravel 5.7.

我使用Laravel 5.7遇到了同样的问题。

If you read the /storage/logs/laravel-yyyy-mm-dd.logfile you will find the error. In my case the error occurred because I forgot a step when linking the StoreLocationrequest in my Controller:

如果您阅读/storage/logs/laravel-yyyy-mm-dd.log文件,您会发现错误。在我的情况下,发生错误是因为我在控制器中链接StoreLocation请求时忘记了一个步骤:

The error in the log:

日志中的错误:

[2018-12-13 09:48:09] local.ERROR: Class App\Requests\StoreLocation does not exist

It is perfectly clear that I started writing code without having my coffee first!

很明显,我没有先喝咖啡就开始编写代码!

The solution:

解决方案:

use App\Http\Requests\StoreLocation;

I added the correct path StoreLocation.php class. After fixing that, everything went back to normal and started sending the 422 response.

我添加了正确的路径 StoreLocation.php 类。修复后,一切恢复正常并开始发送 422 响应。

PS1: To get the logs, ensure debug mode is on: add APP_DEBUG=trueto .env file

PS1:要获取日志,请确保调试模式已打开:将APP_DEBUG=true添加到 .env 文件

PS2: The log file stores the latest entries at the bottom

PS2:日志文件在底部存储最新的条目