Laravel - 自定义使用 Rule 类的错误消息



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

$messages = [
    'email.required' => 'We need to know your e-mail address!',
];
$validator = Validator::make($input, $rules, $messages);

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

例如:

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

完全按照默认翻译中的定义。

相关内容

最新更新