FormRequest 验证失败 returns 500 错误而不是 422 错误(5.2 升级后)
FormRequest failed validation returns 500 error instead of 422 with errors (after 5.2 upgrade)
从 L5.1 更新到 L5.2 后,我不再收到 JSON object 作为对失败的 FormRequest 的响应(即 AJAX post请求)。
通常我会收到类似这样的 422 响应:
[
email: 'E-mail is invalid',
firstname: 'Firstname must be at least 2 characters'
]
但现在我收到一个 500 错误页面:
我已确保我的 AJAX 调用有 application/json
作为 Accept
header。
更新
不,我没有手动捕获这个异常。我正在使用 Laravel 提供的默认 FormRequest。正如他们在文档中所述:在 AJAX 请求期间使用验证方法时,Laravel 不会生成重定向响应。相反,Laravel 生成包含所有验证错误的 JSON 响应。此 JSON 响应将与 422 HTTP 状态代码一起发送。
像这样:php artisan make:request StoreBlogPostRequest
(https://laravel.com/docs/5.1/validation#form-request-validation)
重申一下其他人所说的,您很可能在您的项目中修改了 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.php
和 https://laravel.com/docs/5.2/errors#the-exception-handler 中有关异常处理程序的文档,以确保您按预期处理异常。
@马蒂亚斯!
我最近遇到了同样的问题,我浪费了 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);
}
我在使用 Laravel 5.7 时遇到了同样的问题。
如果您阅读 /storage/logs/laravel-yyyy-mm-dd.log 文件,您会发现错误。在我的例子中,错误发生是因为我在 Controller:
中链接 StoreLocation 请求时忘记了一个步骤
日志中的错误:
[2018-12-13 09:48:09] local.ERROR: Class App\Requests\StoreLocation does not exist
很明显我没有先喝咖啡就开始写代码了!
解决方法:
use App\Http\Requests\StoreLocation;
我添加了正确的路径StoreLocation.phpclass。修复后,一切恢复正常并开始发送 422 响应。
PS1:要获取日志,请确保调试模式已打开:将 APP_DEBUG=true 添加到 .env 文件
PS2: 日志文件在底部存储最新条目
从 L5.1 更新到 L5.2 后,我不再收到 JSON object 作为对失败的 FormRequest 的响应(即 AJAX post请求)。
通常我会收到类似这样的 422 响应:
[
email: 'E-mail is invalid',
firstname: 'Firstname must be at least 2 characters'
]
但现在我收到一个 500 错误页面:
我已确保我的 AJAX 调用有 application/json
作为 Accept
header。
更新
不,我没有手动捕获这个异常。我正在使用 Laravel 提供的默认 FormRequest。正如他们在文档中所述:在 AJAX 请求期间使用验证方法时,Laravel 不会生成重定向响应。相反,Laravel 生成包含所有验证错误的 JSON 响应。此 JSON 响应将与 422 HTTP 状态代码一起发送。
像这样:php artisan make:request StoreBlogPostRequest
(https://laravel.com/docs/5.1/validation#form-request-validation)
重申一下其他人所说的,您很可能在您的项目中修改了 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.php
和 https://laravel.com/docs/5.2/errors#the-exception-handler 中有关异常处理程序的文档,以确保您按预期处理异常。
@马蒂亚斯!
我最近遇到了同样的问题,我浪费了 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);
}
我在使用 Laravel 5.7 时遇到了同样的问题。
如果您阅读 /storage/logs/laravel-yyyy-mm-dd.log 文件,您会发现错误。在我的例子中,错误发生是因为我在 Controller:
中链接 StoreLocation 请求时忘记了一个步骤日志中的错误:
[2018-12-13 09:48:09] local.ERROR: Class App\Requests\StoreLocation does not exist
很明显我没有先喝咖啡就开始写代码了!
解决方法:
use App\Http\Requests\StoreLocation;
我添加了正确的路径StoreLocation.phpclass。修复后,一切恢复正常并开始发送 422 响应。
PS1:要获取日志,请确保调试模式已打开:将 APP_DEBUG=true 添加到 .env 文件
PS2: 日志文件在底部存储最新条目