自定义 Laravel API 验证响应
Customize Laravel API validation responses
我正在用 Laravel 构建一个 REST API,想知道是否有办法在验证时自定义 API 响应。
例如,我在 Laravel 请求中有一个验证规则,表示需要特定字段。
public function rules() {
return [
'title'=>'required|min:4|max:100',
];
}
因此,对于此验证,我在 Postman 中收到这样的错误消息
{
"title": [
"Please enter Ad Title"
]
}
我想要的是像这样自定义该响应..
{
"success": false,
"message": "Validation Error"
"title": [
"Please enter Ad Title"
]
}
所以,错误更具体、更清楚。
那么,如何实现呢?
谢谢!
为 FormRequest
class 提供自定义函数,称为 messages
和 return 使用 dot notation
为特定消息映射的验证消息数组规则:
public function messages()
{
return [
'title.required' => 'Please enter an Ad title',
'title.min' => 'Your title must be at least 4 character'
]
}
返回 success
消息是徒劳的,因为如果它失败了,无论如何执行 ajax
请求时都会抛出 422
错误代码。
至于 message
属性,您将收到它作为有效负载的一部分,其中实际的验证错误将包含在对象中。
可以自定义错误,勾选documentation。你也可以用这种方式验证
$validator = Validator::make($request->all(), [
'title'=>'required|min:4|max:100'
]);
if ($validator->fails()) {
// get first error message
$error = $validator->errors()->first();
// get all errors
$errors = $validator->errors()->all();
}
然后将它们添加到您的回复中,例如
return response()->json([
"success" => false,
"message" => "Validation Error"
"title" => $error // or $errors
]);
我为您的 REST-API 验证找到了解决方案 Laravel FormRequest 验证响应只需编写几行代码即可更改。
在此处输入代码
请将此 two-line 添加到您的 App\Http\Requests\PostRequest 中。php
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
之后在你的文件中添加这个函数
you can change $response variable into your specific manner.
protected function failedValidation(Validator $validator) {
$response = [
'status' => false,
'message' => $validator->errors()->first(),
'data' => $validator->errors()
];
throw new HttpResponseException(response()->json($response, 200));
}
我正在用 Laravel 构建一个 REST API,想知道是否有办法在验证时自定义 API 响应。
例如,我在 Laravel 请求中有一个验证规则,表示需要特定字段。
public function rules() {
return [
'title'=>'required|min:4|max:100',
];
}
因此,对于此验证,我在 Postman 中收到这样的错误消息
{
"title": [
"Please enter Ad Title"
]
}
我想要的是像这样自定义该响应..
{
"success": false,
"message": "Validation Error"
"title": [
"Please enter Ad Title"
]
}
所以,错误更具体、更清楚。
那么,如何实现呢?
谢谢!
为 FormRequest
class 提供自定义函数,称为 messages
和 return 使用 dot notation
为特定消息映射的验证消息数组规则:
public function messages()
{
return [
'title.required' => 'Please enter an Ad title',
'title.min' => 'Your title must be at least 4 character'
]
}
返回 success
消息是徒劳的,因为如果它失败了,无论如何执行 ajax
请求时都会抛出 422
错误代码。
至于 message
属性,您将收到它作为有效负载的一部分,其中实际的验证错误将包含在对象中。
可以自定义错误,勾选documentation。你也可以用这种方式验证
$validator = Validator::make($request->all(), [
'title'=>'required|min:4|max:100'
]);
if ($validator->fails()) {
// get first error message
$error = $validator->errors()->first();
// get all errors
$errors = $validator->errors()->all();
}
然后将它们添加到您的回复中,例如
return response()->json([
"success" => false,
"message" => "Validation Error"
"title" => $error // or $errors
]);
我为您的 REST-API 验证找到了解决方案 Laravel FormRequest 验证响应只需编写几行代码即可更改。 在此处输入代码
请将此 two-line 添加到您的 App\Http\Requests\PostRequest 中。php
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
之后在你的文件中添加这个函数
you can change $response variable into your specific manner.
protected function failedValidation(Validator $validator) {
$response = [
'status' => false,
'message' => $validator->errors()->first(),
'data' => $validator->errors()
];
throw new HttpResponseException(response()->json($response, 200));
}