阅读(1613) (12)

Laravel 8 自定义错误信息

2021-06-25 11:18:23 更新

如果有需要,您可以使用自定义的错误信息来替换默认的错误信息。有几种方法可以指定自定义错误信息。首先,您可以将自定义信息作为 Validator::make 方法的第三个参数:

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

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

在该例中,:attribute 占位符将被验证字段的实际名称替换。您亦可在验证消息中使用其他占位符。例如:

$messages = [
    'same' => 'The :attribute and :other must match.',
    'size' => 'The :attribute must be exactly :size.',
    'between' => 'The :attribute value :input is not between :min - :max.',
    'in' => 'The :attribute must be one of the following types: :values',
];