gpt4 book ai didi

Laravel 请求验证消息格式化

转载 作者:行者123 更新时间:2023-12-04 17:59:47 26 4
gpt4 key购买 nike

使用 Laravel Form Request Validation ,
如何自定义响应格式

例如,它将错误消息显示为:

{
"password":[
"The password field is required."
],
"password_confirmation":[
"The password confirmation field is required."
]
}

我想将所有错误信息放入描述属性中,类似的东西。

{
"status":false,
"description":[
"The password field is required.",
"The password confirmation field is required."
]
}

最佳答案

它就在您提供的文档链接的正下方:

You may customize the error messages used by the form request by overriding the messages method. This method should return an array of attribute / rule pairs and their corresponding error messages.

所以,把这个放在你的请求中:

public function messages()
{
return [
'password.required' => 'The password field is required.',
'password_confirmation.required' => 'The password confirmation field is required.',
];
}

关于Laravel 请求验证消息格式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36963501/

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