0

I'm validating my API request in Laravel controller. It consists of an array of objects that each of the item needed to be validated.

I tried the nested array validation and tried creating a separate request validation class but was not able to succeed.

{
    "total" : 250.00,
    "merchant_id" : 1,
    "discount" : 0,
    "items" :  {
        [id: 1, quantity: 25, notes: "some string A"],
        [id: 2, quantity: 10, notes: "some string B"],
        [id: 3, quantity: 5, notes: "some string C"]
    }
}

Each parameter of the main object (total, merchant_id, discount) and also the nested array parameters (id, quantity, notes) needed to be validated

2 Answers 2

3

Use the .* notation as specified in https://laravel.com/docs/5.8/validation#validating-arrays

Sign up to request clarification or add additional context in comments.

Comments

1

Let's say all of them are required. you can validate it like this:

$validator = Validator::make($request->all(), [
    'total' => 'required',
    'merchant_id' => 'required',
    'discount' => 'required',
    'items.*.id' => 'required',
    'items.*.quantity' => 'required',
    'items.*.notes' => 'required',
]);

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.