如何使用此格式返回自定义错误消息?
$this->validate($request, [
'thing' => 'required'
]);
答案 0 :(得分:36)
要获取自定义错误消息,您需要在第三个参数上传递自定义错误消息,例如
$this->validate(
$request,
['thing' => 'required'],
['thing.required' => 'this is my custom error message for required']
);
答案 1 :(得分:21)
对于多字段,角色和字段角色特定消息
$this->validate(
$request,
[
'uEmail' => 'required|unique:members',
'uPassword' => 'required|min:8'
],
[
'uEmail.required' => 'Please Provide Your Email Address For Better Communication, Thank You.',
'uEmail.unique' => 'Sorry, This Email Address Is Already Used By Another User. Please Try With Different One, Thank You.',
'uPassword.required' => 'Password Is Required For Your Information Safety, Thank You.',
'uPassword.min' => 'Password Length Should Be More Than 8 Character Or Digit Or Mix, Thank You.',
]
);
答案 2 :(得分:0)
您需要先在视图页面中添加以下行,以显示错误消息:
<div class="row">
<div class="col-md-4 col-md-offset-4 error">
<ul>
@foreach($errors->all() as $error)
<li>{{$error}}</li>
@endforeach
</ul>
</div>
</div>
这是一个演示控制器,错误消息将显示在该页面上:
public function saveUser(Request $request)
{
$this->validate($request,[
'name' => 'required',
'email' => 'required|unique:users',
]);
$user=new User();
$user->name= $request->Input(['name']);
$user->email=$request->Input(['email']);
$user->save();
return redirect('getUser');
}
详细信息您可以关注Blog post。 除此之外,您还可以关注laravel官方文档Validation。
答案 3 :(得分:0)
https://laravel.com/docs/5.3/validation#working-with-error-messages
$messages = [
'required' => 'The :attribute field is required.',
];
$validator = Validator::make($input, $rules, $messages);
&#34;在大多数情况下,您可能会在语言文件中指定自定义消息,而不是直接将它们传递给Validator。为此,请在resources / lang / xx / validation.php语言文件中将消息添加到自定义数组。&#34;