0

我验证了一个二维数组,当验证器失败时,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”键?

4

2 回答 2

0

找到了。$key将包含“1.0”和 $error 消息。

   foreach ($errors->getMessages() as $key => $error) {


   }
于 2019-03-29T11:54:22.467 回答
0

在 中MessageBag,有一个方法keys()可以获取所有MessageBag键,有关可用于该类的方法的更多信息MessageBag,请查看MessageBag laravel api doc

于 2019-03-29T11:58:04.577 回答