Laravel - 使用规则 class 自定义错误消息

Laravel - Customize error message that using Rule class

根据Laravel的文档,编辑错误信息的方法是这样的:

$messages = [
    'email.required' => 'We need to know your e-mail address!',
];

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

但是如果规则使用 Rule class 怎么办?

例如:

$rules = [
    'img_type'      => ['required', Rule::in(['png', 'jpeg', 'gif'])],
];

$messages = [
    'img_type.{what-to-type-here-for-Rule::in}' => 'Invalid image type',
];

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

如上面的例子,img_type.{what-to-type-here-for-Rule::in},我不知道如何为Rule::in指定自定义错误信息...

规则刚好叫做 in。所以这就是你必须使用的。

$messages = [
    'img_type.in' => 'Invalid image type',
];

default translations 中的定义完全一致。