验证器 - return 所有错误消息
Validator - return all error messages
目前 Laravel 中的验证器只显示 return 每个字段一条错误消息,尽管该字段可能有多个规则和消息。 (注意:我目前正在将一个空数组作为 $data 传递给 Validator::make)
我想做的是构建一个包含每个字段的规则和消息的数组,这些规则和消息可能会被重新用于前端验证。像这样:
{
"name": {
"required": [
"The name field is required."
],
"max:255": [
"The name field may not be greater than 255."
]
},
"email": {
"required": [
"The email field is required."
],
"email": [
"The email field must be a valid email address."
],
"max:255": [
"The email field may not be greater than 255."
]
}
}
Illuminate\Validation\Validator 中的 getMessage 方法看起来让我接近于能够自己构建一些东西,但它是一个受保护的方法。
有谁知道让 Validator 实例输出所有规则和消息的方法吗?
Currently the Validator in Laravel only appears to return one error message per field, despite the field potentially having multiple rules and messages.
一旦单个验证规则失败,给定字段的验证就会停止。这就是每个字段只收到一条错误消息的原因。
在您提供的示例中获取验证消息时,Laravel 的验证器不提供此类选项,但您可以通过扩展 Validator class.
首先,创建新的 class:
<?php namespace Your\Namespace;
use Illuminate\Validation\Validator as BaseValidator;
class Validator extends BaseValidator {
public function getValidationMessages() {
$messages = [];
foreach ($this->rules as $attribute => $rules) {
foreach ($rules as $rule) {
$messages[$attribute][$rule] = $this->getMessage($attribute, $rule);
}
}
return $messages;
}
}
如您所见,输出与您的示例略有不同。不需要 return 给定属性和规则的消息数组,因为数组中总是只有一条消息,所以我只是在那里存储一个字符串。
其次,您需要确保使用了验证器 class。为了实现这一点,您需要使用 Validator facade:
注册您自己的验证器解析器
Validator::resolver(function($translator, array $data, array $rules, array $messages, array $customAttributes) {
return new \Your\Namespace\Validator($translator, $data, $rules, $messages, $customAttributes);
});
您可以在 AppServiceProvider::boot() 方法中执行此操作。
现在,为了获取给定验证器的验证消息,您只需调用:
Validator::make($data, $rules)->getValidationMessages();
请记住,此代码尚未经过测试。如果您发现代码有任何问题或拼写错误,请告诉我,我将非常乐意为您解决这个问题。
目前 Laravel 中的验证器只显示 return 每个字段一条错误消息,尽管该字段可能有多个规则和消息。 (注意:我目前正在将一个空数组作为 $data 传递给 Validator::make)
我想做的是构建一个包含每个字段的规则和消息的数组,这些规则和消息可能会被重新用于前端验证。像这样:
{
"name": {
"required": [
"The name field is required."
],
"max:255": [
"The name field may not be greater than 255."
]
},
"email": {
"required": [
"The email field is required."
],
"email": [
"The email field must be a valid email address."
],
"max:255": [
"The email field may not be greater than 255."
]
}
}
Illuminate\Validation\Validator 中的 getMessage 方法看起来让我接近于能够自己构建一些东西,但它是一个受保护的方法。
有谁知道让 Validator 实例输出所有规则和消息的方法吗?
Currently the Validator in Laravel only appears to return one error message per field, despite the field potentially having multiple rules and messages.
一旦单个验证规则失败,给定字段的验证就会停止。这就是每个字段只收到一条错误消息的原因。
在您提供的示例中获取验证消息时,Laravel 的验证器不提供此类选项,但您可以通过扩展 Validator class.
首先,创建新的 class:
<?php namespace Your\Namespace;
use Illuminate\Validation\Validator as BaseValidator;
class Validator extends BaseValidator {
public function getValidationMessages() {
$messages = [];
foreach ($this->rules as $attribute => $rules) {
foreach ($rules as $rule) {
$messages[$attribute][$rule] = $this->getMessage($attribute, $rule);
}
}
return $messages;
}
}
如您所见,输出与您的示例略有不同。不需要 return 给定属性和规则的消息数组,因为数组中总是只有一条消息,所以我只是在那里存储一个字符串。
其次,您需要确保使用了验证器 class。为了实现这一点,您需要使用 Validator facade:
注册您自己的验证器解析器Validator::resolver(function($translator, array $data, array $rules, array $messages, array $customAttributes) {
return new \Your\Namespace\Validator($translator, $data, $rules, $messages, $customAttributes);
});
您可以在 AppServiceProvider::boot() 方法中执行此操作。
现在,为了获取给定验证器的验证消息,您只需调用:
Validator::make($data, $rules)->getValidationMessages();
请记住,此代码尚未经过测试。如果您发现代码有任何问题或拼写错误,请告诉我,我将非常乐意为您解决这个问题。