-1

帮助请。“如何在 laravel 中添加错误用户错误”我想显示 JSON 文件 - 错误 $regex - 我需要的规则规则在不正确的 sourceUrl 上出现错误

           $this->validate($request,[
            'title' => 'required|min:10|max:250', //work
            'subTitle' =>'sometimes|present|nullable|min:10|max:250', //work
            'message' => 'required|min:10',//work
            'recommendPic' => 'present|nullable', //work
            'pic' => 'required|sometimes', //file - check upload file,image need fix
            'sourceUrl' =>'required|regex:'.$regex,
       ],[
            'sourceUrl.regex:'.$regex=>'mistake',
       ]);
4

2 回答 2

0

您需要返回失败的验证消息,如下所示:

$validation = Validator::make($request->all(), [
    'title' => 'required|min:10|max:250', //work
    'subTitle' =>'sometimes|present|nullable|min:10|max:250', //work
    'message' => 'required|min:10',//work
    'recommendPic' => 'present|nullable', //work
    'pic' => 'required|sometimes', //file - check upload file,image need fix
    'sourceUrl' =>'required|regex:'.$regex,
]);

// if validation fails
if ($validation->fails()) {
  return response()->json([
    'status' => 'failure',
    'errors' => $validation->errors()
  ], 400);
}

// validation passes
return response()->json(['status' => 'success'], 200);
于 2020-08-06T08:07:00.613 回答
0

'sourceUrl.regex:'.$regex=>'mistake',不是有效的 PHP 代码。

它应该是这样的:'sourceUrl.regex' => 'regex mistake'

这将使用错误消息重定向回来regex mistake

在您看来,您可以使用 打印消息@error('sourceUrl') {{ $message }}@enderror

于 2020-08-06T08:06:24.097 回答