如何在Laravel验证中添加错误消息



帮助plz。"如何在laravel中添加错误用户错误;我想显示JSON文件-错误$regex-请求规则我需要在不正确的源URL 上出错

$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',
]);

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

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

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

它应该类似于:'sourceUrl.regex' => 'regex mistake'

这会重定向回错误消息regex mistake

在您的视图中,您可以使用@error('sourceUrl') {{ $message }}@enderror打印邮件。

相关内容

  • 没有找到相关文章

最新更新