我想知道是否可以为特定字段创建自定义验证消息。例如,我有一个名为 firstname, laravel 的字段,所以验证错误消息带有 if firstname(不是大写字母,全部在一起),所以它看起来很糟糕。我想为这个字段创建一个特定的消息,它必须在验证中.php因为我的网站有不同的语言。可能吗?
如果我有一个名为电子邮件的字段?我知道电子邮件已经是验证消息,因此我可以仅针对名为电子邮件的字段创建自定义验证消息。无论他们是否具有电子邮件验证条件。
是的,你可以。
使用此自定义消息添加特定于字段的自定义消息
//field_rule => 'your message'
$messages = array(
'email_required' => 'The :attribute field is required.'
);
是的,您可以像这样使用此自定义消息:
$messages = [
'required' => 'The :attribute field is required.',
'birthdate.required' => 'The BIRTHDATE field is required.'
];
$validator = Validator::make($request->all(), [
'name' => 'required',
'birthdate' => 'required',
], $messages);
您可以通过自定义错误消息设置更改验证消息 ,详细信息
现在根据您的语言,您只需更改$messages
数组
$messages = array(
'required' => 'The :attribute field is required.',
'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',
);
$validator = Validator::make($input, $rules, $messages);