【问题标题】:How to edit the default validation message in laravel?如何在 laravel 中编辑默认验证消息?
【发布时间】:2020-04-29 19:11:54
【问题描述】:

我在下面有服务器验证:

$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() 错误。返回的日志如下所示:

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

【问题讨论】:

标签: php laravel laravel-validation


【解决方案1】:

我不知道您的目标,但如果您找到文件。它在目录中 ..\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;
}

【讨论】:

  • 感谢它有效!我想在这里做一些事情,这就是我问的原因。
猜你喜欢
  • 2019-10-24
  • 2020-08-15
  • 2012-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-21
  • 2020-01-17
  • 2018-12-02
相关资源
最近更新 更多