Laravel Nova - 看不到验证错误消息
Laravel Nova - Can`t see validation error messages
我正在使用 Nova 仪表板开发一个项目。出于某种原因,我看不到验证错误消息。
如果出现验证错误,我可以在浏览器控制台中将其视为异常。但在 Nova UI 中不是。
如果表单的所有字段都输入正确,我可以看到成功消息。
我是 Nova 的新手,谁能帮我调试这个问题?我的意思是我不知道去哪里寻找解决这个问题
来自浏览器的错误跟踪:
{
"errors":"Sorry, something went wrong.",
"exception":"Illuminate\Validation\ValidationException",
"message":"The given data was invalid.",
"trace":[{
"file":"\/home\/ausvacs\/public_html\/nova\/src\/PerformsValidation.php",
"line":18,
"function":"validate",
"class":"Illuminate\Validation\Validator",
"type":"->",
"args":[]
}]
}
Agency nova模型的Fields方法(Table name:agency):
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Name')
->sortable()
->rules('required', 'string'),
];
}
浏览器控制台出错:
浏览器网络选项卡异常:
问题出在处理程序 (app/Exceptions/Handler.php) 中。不确定之前的开发者是否更新了这个功能。不管怎样,这个函数中的问题是:
- 验证异常时的状态码returning是400,那需要422。然后只有Vue组件会显示验证信息。
- 此外,这里的错误被推送到 "response" 数组的 "validation" 索引。但是 Vue 组件正在检查 "response" 数组的 "errors" 索引。
public function render($request, Exception $exception)
{
if ($request->ajax() || $request->wantsJson()) {
$response = ['errors' => 'Sorry, something went wrong.'];
$status = 400;
if ($this->isHttpException($exception)) {
$status = $exception->getStatusCode();
}
if (get_class($exception) == 'Illuminate\Validation\ValidationException') {
$response['validation'] = $exception->validator->errors();
}
return response()->json($response, $status);
}
return parent::render($request, $exception);
}
当我在验证异常的情况下将代码更新为 return 错误代码为 422 并将错误推送到响应的 "errors" 索引而不是 "validation" 索引时,问题得到解决。
我正在使用 Nova 仪表板开发一个项目。出于某种原因,我看不到验证错误消息。
如果出现验证错误,我可以在浏览器控制台中将其视为异常。但在 Nova UI 中不是。
如果表单的所有字段都输入正确,我可以看到成功消息。
我是 Nova 的新手,谁能帮我调试这个问题?我的意思是我不知道去哪里寻找解决这个问题
来自浏览器的错误跟踪:
{
"errors":"Sorry, something went wrong.",
"exception":"Illuminate\Validation\ValidationException",
"message":"The given data was invalid.",
"trace":[{
"file":"\/home\/ausvacs\/public_html\/nova\/src\/PerformsValidation.php",
"line":18,
"function":"validate",
"class":"Illuminate\Validation\Validator",
"type":"->",
"args":[]
}]
}
Agency nova模型的Fields方法(Table name:agency):
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Name')
->sortable()
->rules('required', 'string'),
];
}
浏览器控制台出错:
浏览器网络选项卡异常:
问题出在处理程序 (app/Exceptions/Handler.php) 中。不确定之前的开发者是否更新了这个功能。不管怎样,这个函数中的问题是:
- 验证异常时的状态码returning是400,那需要422。然后只有Vue组件会显示验证信息。
- 此外,这里的错误被推送到 "response" 数组的 "validation" 索引。但是 Vue 组件正在检查 "response" 数组的 "errors" 索引。
public function render($request, Exception $exception)
{
if ($request->ajax() || $request->wantsJson()) {
$response = ['errors' => 'Sorry, something went wrong.'];
$status = 400;
if ($this->isHttpException($exception)) {
$status = $exception->getStatusCode();
}
if (get_class($exception) == 'Illuminate\Validation\ValidationException') {
$response['validation'] = $exception->validator->errors();
}
return response()->json($response, $status);
}
return parent::render($request, $exception);
}
当我在验证异常的情况下将代码更新为 return 错误代码为 422 并将错误推送到响应的 "errors" 索引而不是 "validation" 索引时,问题得到解决。