数组laravel验证“ *”返回true

时间:2019-12-09 18:07:30

标签: laravel validation vue.js post request

我正在尝试验证数组
数组:

location_id: [
     {id: 1, name: "Line 1"}, 
     {id: 2, name: "Line 2"}, 
     {id: 3, name: "Flex 1"}, 
     {id: 4, name: "Flex 2"}
],
name: "Failure 1",
station_id: [
     {id: 1, name: "Station 1"}, 
     {id: 2, name: "Station 2"}
]

当数据发送到服务器时,它返回true
验证码:

 $rules = [
     'location_id.*.id'  =>  'required',
     'station_id.*.id'  =>   'required'
 ];

 Validator::make($request->all(),$rules)->passes());

如果数组false为空或location_id,则应返回location_id[0].id == null,但如果数组为空,则返回true。

如果按以下方式指定规则,则可以正常工作,但有时我会在location_idstation_id内添加2,3个或更多数组

 $rules = [
     'location_id.0.id'  =>  'required',
     'station_id.0.id'  =>   'required'
 ];

1 个答案:

答案 0 :(得分:0)

尝试此规则

$rules = [
     'location_id'  =>  'required|array|min:1',
     'location_id.*.id'  =>   'required',
     'station_id.*.id'  =>   'required'
];

如果'station_id' => 'required|array|min:1'为空,如果要返回$rules,则将false添加到station_id