0

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: enter image description here

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?

7
  • Just for sanity sake, dd($reques->all());, let's see what's coming in from the request Commented Dec 29, 2022 at 16:17
  • how to do you send data? what is the content type of your request? Commented Dec 29, 2022 at 16:24
  • 1
    You seems to send a string with [ ] Commented Dec 29, 2022 at 16:34
  • 1
    your obvious mistake is in validation rule . you defined product_ids* but it must be product_ids.* and same about product_quantities . Commented Dec 29, 2022 at 16:43
  • @zohrehda this explains the "The product_quantities must be a number." error. But not the array validation, right? I am sending the data using uno (dart) post request. It seems to transform the data to a string and then encode it to a list of ints using utf8. Commented Dec 30, 2022 at 3:17

2 Answers 2

1

The request should be passed as an object when you pass data as an array.

In Postman/AJAX/JS

'Content-Type': 'application/json'

JSON.stringify(); # for async  request

Example

sendProductIds(productIds) {
    body: JSON.stringify({ product_ids: productIds })
}

sendProductIds([1, 2, 3]);

And Validation should be

$validator = Validator::make($request->all(), [
    'product_ids' => 'required|array',
    'product_ids.*' => 'integer|exists:products,id',
]);
Sign up to request clarification or add additional context in comments.

2 Comments

(without changing the validation, since I believe the issue is only related to the second error) I get the same response i.imgur.com/ATwAZKu.png
Content-type should be in headers. You must change this 'product_ids.*' => 'integer|exists:products,id',
0

There is a difference in validation of an array parameter and elements in the array parameter:

$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')]);

You need to put a . between the array parameter and *. That refers to elements in the array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.