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

问题描述 投票:0回答:1

我下面有服务器验证。

$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() 的错误。返回的日志如下。enter image description here

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

php laravel laravel-validation
1个回答
2
投票

我不知道你的目标是什么,但如果你找到这个文件。它在目录中 .../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;
}
© www.soinside.com 2019 - 2024. All rights reserved.