Is there anyway to validate the index array of objects in laravel with the validator?
Here is my post data:
[
{
"order_id": 601696,
"is_delay": 0,
"ship_date": null,
"times": 1,
"warehouse_id": 237,
"ship_type": 1,
"package": [
{
"sku": "JD-XL102",
"order_item_id": 772447,
"qty": 2
}
]
},
{
"order_id": 601696,
"is_delay": 0,
"ship_date": null,
"times": 1,
"warehouse_id": 251,
"ship_type": 1,
"package": [
{
"sku": "JD-D16",
"order_item_id": 772447,
"qty": 1
},
{
"sku": "JD-D16-GC",
"order_item_id": 772448,
"qty": 1
}
]
}
]
Then i want to validate it like this :
public function rules()
{
return [
'*.*.order_id' => 'required|integer',
'*.*.is_delay' => 'required|boolean',
'*.*.ship_date' => 'nullable|datetime',
'*.*.times' => 'required|integer|min:1',
'*.*.warehouse_id' => 'required|integer',
'*.*.ship_type' => 'required|integer',
'*.*.package' => 'required|array',
'*.*.package.*.sku' => 'required|string',
'*.*.package.*.qty' => 'required|integer|min:1',
];
}
Howevere it not works.
I know i can add a prefix field to fix it, but i just want to know if laravel support the index array to validate without the prefix key?
{
"rows": [
{...}
]
}
public function rules()
{
return [
'row' => 'required|array',
'row.*.order_id' => 'required|integer',
'row.*.is_delay' => 'required|boolean',
'row.*.ship_date' => 'nullable|datetime',
'row.*.times' => 'required|integer|min:1',
'row.*.warehouse_id' => 'required|integer',
'row.*.ship_type' => 'required|integer',
'row.*.package' => 'required|array',
'row.*.package.*.sku' => 'required|string',
'row.*.package.*.qty' => 'required|integer|min:1',
];
}
PHP version: v7.4.33
Laravel version: v6.20.44