如何在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.