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

cma*_*mac 17 php validation laravel

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

$this->validate($request, [
  'thing' => 'required'
]);
Run Code Online (Sandbox Code Playgroud)

Imt*_*bel 43

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

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

  • 我们如何为二维数组添加自定义消息? (2认同)

小智 24

对于多字段,角色和字段特定于角色的消息

$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.',
        ]
    );
Run Code Online (Sandbox Code Playgroud)

  • 谢谢伙计,它帮助我在 laravel 5.5 中添加了自定义错误消息... :-) (2认同)