5.5版本中的Laravel阵列验证

时间:2017-10-30 09:44:20

标签: php laravel laravel-5

验证码

'event' => 'required|array',
'event.*.start_date_time' => 'required|date_format:Y-m-d|after:today',
'event.*.end_date_time' => 'required|date_format:Y-m-d|after:today',
'event.*.description' => 'required|spam_free',

错误消息代码

if ($messages->has('event')) {
    $fieldId = 'event';
    $errors [] = getErrorMessage($fieldId, $messages->first($fieldId));
}
if ($messages->has('event.*.start_date_time')) {
    $fieldId = 'event.*.start_date_time';
    $errors [] = getErrorMessage($fieldId, $messages->first($fieldId));
}
if ($messages->has('event.*.end_date_time')) {
    $fieldId = 'event.*.end_date_time';
    $errors [] = getErrorMessage($fieldId, $messages->first($fieldId));
}
if ($messages->has('event.*.description')) {
    $fieldId = 'event.*.description';
    $errors [] = getErrorMessage($fieldId, $messages->first($fieldId));
}

错误消息

{
   "fieldld": "event.*.start_date_time",
   "message": "The event.0.start_date_time field is required."
},
{
    "fieldld": "event.*.end_date_time",
    "message": "The event.0.end_date_time field is required."
},
{
    "fieldld": "event.*.description",
    "message": "The event.0.description field is required."
},

阵列

event[0].start_date_time:2017-03-22 15:42:22
event[0].end_date_time:2017-03-22 15:42:22
event[0].description:test description

任何人都可以帮助我为什么不验证数组以及为什么我没有收到正确的错误信息。

提前致谢

2 个答案:

答案 0 :(得分:0)

对不起,伙计们我以错误的格式传递数组。

event[0][start_date_time]:2017-12-12
event[0][end_date_time]:2017-12-12
event[0][description]:asfdsfdsf

答案 1 :(得分:0)

对数组使用Validator。

 $validate = Validator::make($data,[
            'start_time' => 'required|after:today',
        ]);

if($validate->fails()){

 return $validate->getMessageBag()->toArray();

}

https://laravel.com/docs/5.5/validation#validating-arrays