I have an API built with Laravel. It does the following validation:
$validator = Validator::make($request->all(), [
'product_ids' => 'required|array',
'product_ids*' => 'required|exists:products,id',
'product_quantities' => 'required|array',
'product_quantities*' => 'numeric',
], [
'order_type.in' => trans('validation.order_type'),
]);
I am sending the following data in the request:
As text:
product_ids:[697,339]
product_quantities:[3,1]
However, I get as a response, 422:
{
"errors": {
"product_ids": [
"The product_ids must be an array."
],
"product_quantities": [
"The product_quantities must be an array.",
"The product_quantities must be a number."
]
}
}
The same results appear using postman, thunder client and uno (dart). However, it works if I use axios (JS) with the same apparent data.
Any clue why the data I send is not always recognized as an array?
2
Answers
There is difference in validation of a array parameter and elements in the array parameter:
You need to put a
.
between the array parameter and*
. That reffers elements in the array.The request should be passed as an object when you pass data as an array.
In Postman/AJAX/JS
Example
And Validation should be