我正在构建一个API,我正在验证StoreLesson.php中的输入字段
public function rules()
{
return [
'title' => 'required',
'body' => 'required',
];
}
我正在使用postman
来测试API,一切正常但是当我发送带空字段的POST请求时,在webview的postman
控制台中我被重定向到{{1} }
welcom.blade.php
我想将验证器错误显示(返回)为json
谢谢你
答案 0 :(得分:1)
使用这样的验证器:
$validator = Validator::make($data, $rules);
if ($validator->fails())
return response()->json($validator);
答案 1 :(得分:0)
Create StoreLessons Request inside app/Http/Requests.And call this request in LessonController.php you did.
----------
class StoreLessons extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'required',
'body' => 'required',
];
}
/**
* @param array $errors
*
* @return JsonResponse
*/
public function response(array $errors)
{
return response($errors);
}
/**
* @param Validator $validator
*
* @return mixed
*/
protected function formatErrors(Validator $validator)
{
return $validator->errors()->all();
}