如何编辑 laravel 中的默认验证消息?

How to edit the default validation message in laravel?

我在下面进行了服务器验证:

$this->validate([
    'first_name' => ['required','min:2','max:30',new PersonNameRule],
    'last_name' => ['required','min:2','max:30',new PersonNameRule],
    'username' => ['required','confirmed',new UsernameRule],
    'password' => 'required|confirmed|min:6|max:20',
    'birthdate' => ['required','date',new EligibleAgeRule(21)]
]);

在前端,我故意使验证失败并 console.log() 错误。返回的日志如下所示:

现在我的问题是,消息(给定的数据无效)来自哪里?我可以定制吗?

我不知道你的 objective 但如果你找到了文件。它在目录中 ..\Illuminate\Validation\ValidationException.php

您可以编辑您在 __construct 方法中提到的消息。请参阅下面的示例:

public function __construct($validator, $response = null, $errorBag = 'default') {
    parent::__construct('The given data was invalid.'); //change the message here

    $this->response = $response;
    $this->errorBag = $errorBag;
    $this->validator = $validator;
}