我正在尝试验证数组
数组:
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_id
或station_id
内添加2,3个或更多数组
$rules = [
'location_id.0.id' => 'required',
'station_id.0.id' => 'required'
];
答案 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
。