gpt4 book ai didi

Laravel Nova - 看不到验证错误消息

转载 作者:行者123 更新时间:2023-12-05 07:28:33 25 4
gpt4 key购买 nike

我正在使用 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方法(表名:agency):

public function fields(Request $request)
{
return [
ID::make()->sortable(),

Text::make('Name')
->sortable()
->rules('required', 'string'),

];
}

浏览器控制台错误:

enter image description here

浏览器网络选项卡异常:

enter image description here

最佳答案

问题出在处理程序 (app/Exceptions/Handler.php) 中。不确定之前的开发者是否更新了这个功能。无论如何,这个函数中的问题是:

  • 验证异常时返回的状态码是400,那需要422,那么只有Vue组件会显示验证信息。
  • 此外,这里的错误被推送到“响应”数组的“验证”索引。但是 Vue 组件正在检查“响应”数组的“错误”索引。

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);
}

当我更新代码以在发生验证异常时将错误代码返回为 422 并将错误推送到响应的“错误”索引而不是“验证”索引时,问题得到解决。

关于Laravel Nova - 看不到验证错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53312555/

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