如果有需要,您可以设置自定义的错误信息取代默认错误信息。这里有几种方式可以设定自定义消息。
传递自定义消息进验证器
$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 must be between :min - :max.",
"in" => "The :attribute must be one of the following types: :values",
];
为特定属性赋予一个自定义信息
有时您只想为一个特定字段指定一个自定义错误信息:
$messages = [
"email.required" => "We need to know your e-mail address!",
];
在语言包文件中指定自定义消息
某些状况下,您可能希望在语言包文件中设定您的自定义消息,而非直接将他们传递给 Validator。要达到这个目的,将您的信息增加至 resources/lang/xx/validation.php 文件的 custom 数组中。
"custom" => [
"email" => [
"required" => "We need to know your e-mail address!",
],
],