如何从控制器方法验证返回自定义错误消息

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

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

答案 1

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

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

答案 2

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

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

推荐