php Laravel 5.3 使用 $this->validate() 返回自定义错误消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40067212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:06:22  来源:igfitidea点击:

Laravel 5.3 return custom error message using $this->validate()

phplaravelvalidation

提问by cmac

How to return a custom error message using this format?

如何使用此格式返回自定义错误消息?

$this->validate($request, [
  'thing' => 'required'
]);

回答by Imtiaz Pabel

to get custom error message you need to pass custom error message on third parameter,like that

要获取自定义错误消息,您需要在第三个参数上传递自定义错误消息,就像这样

$this->validate(
    $request, 
    ['thing' => 'required'],
    ['thing.required' => 'this is my custom error message for required']
);

回答by Md. Noor-A-Alam Siddique

For Multiple Field, Role and Field-Role Specific Message

对于多字段、角色和字段-角色特定消息

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

回答by user634545

https://laravel.com/docs/5.3/validation#working-with-error-messages

https://laravel.com/docs/5.3/validation#working-with-error-messages

$messages = [
    'required' => 'The :attribute field is required.',
];

$validator = Validator::make($input, $rules, $messages);

"In most cases, you will probably specify your custom messages in a language file instead of passing them directly to the Validator. To do so, add your messages to custom array in the resources/lang/xx/validation.php language file."

“在大多数情况下,您可能会在语言文件中指定您的自定义消息,而不是将它们直接传递给验证器。为此,请将您的消息添加到 resources/lang/xx/validation.php 语言文件中的自定义数组。”

回答by Hola

You need to first add following lines in view page where you want to show the Error message:

您需要首先在要显示错误消息的视图页面中添加以下行:

<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>

Here is a demo controller by which error message will appear on that page:

这是一个演示控制器,错误消息将显示在该页面上:

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');
 }

For details You can follow the Blog post. Besides that you can follow laravel official doc as well Validation.

有关详细信息,您可以关注博客文章。除此之外,您还可以关注 laravel 官方文档以及Validation