Laravel 5.8 访问错误消息包进行数组验证
Laravel 5.8 access error messageBag for array validation
我验证了一个二维数组,当验证器失败时,validator->errors()
包含:
MessageBag {#1010 ▼
#messages: array:8 [▼
"1.0" => array:1 [▼
0 => "The 1.0 does not match the format d/m/Y."
]
"2.0" => array:1 [▶]
"1.1" => array:1 [▶]
"2.1" => array:1 [▶]
"1.2" => array:1 [▶]
"2.2" => array:1 [▶]
"1.4" => array:1 [▶]
"2.4" => array:1 [▶]
]
#format: ":message"
}
现在我可以像这样访问所有错误:
foreach ($errors->all() as $error) {
}
这会将消息放入 $error = "The 1.0 does not match the format d/m/Y.";
如何访问“1.0”密钥?
找到了。 $key
将包含“1.0”和 $error 消息。
foreach ($errors->getMessages() as $key => $error) {
}
在 MessageBag
中,有一个方法 keys()
用它你可以获得所有 MessageBag
键,有关可以与 [=10= 一起使用的方法的更多信息] class,请检查MessageBag laravel api doc
我验证了一个二维数组,当验证器失败时,validator->errors()
包含:
MessageBag {#1010 ▼
#messages: array:8 [▼
"1.0" => array:1 [▼
0 => "The 1.0 does not match the format d/m/Y."
]
"2.0" => array:1 [▶]
"1.1" => array:1 [▶]
"2.1" => array:1 [▶]
"1.2" => array:1 [▶]
"2.2" => array:1 [▶]
"1.4" => array:1 [▶]
"2.4" => array:1 [▶]
]
#format: ":message"
}
现在我可以像这样访问所有错误:
foreach ($errors->all() as $error) {
}
这会将消息放入 $error = "The 1.0 does not match the format d/m/Y.";
如何访问“1.0”密钥?
找到了。 $key
将包含“1.0”和 $error 消息。
foreach ($errors->getMessages() as $key => $error) {
}
在 MessageBag
中,有一个方法 keys()
用它你可以获得所有 MessageBag
键,有关可以与 [=10= 一起使用的方法的更多信息] class,请检查MessageBag laravel api doc