1

i have a nested array to validate which i looked at other questions and valited that . but my problem begins when i multiple the array like below :

{
    "items": [
      {
            "sender": {
                "firstName": "firstName",
                "lastName": "lastName",


            },
            "items": [
                {
                    "weight": {
                        "value": 1000
                    }
                }
            ]
        },
            {
            "sender": {
                "firstName": "firstName",
                "lastName": "lastName",


            },
            "items": [
                {
                    "weight": {
                        "value": 1000
                    }
                }
            ]
        }
    ]
}

now what i want to do is to validate senders and value to exists and check types of them . what i have tried so far is : first i send the $data = $request->get('items'); to the validator and then .

    public function rules()
    {
        return [
            "sender.*.firstName" => "required|string|max:17",

but i all the time get this error on validation :

{
    "detail": "",
    "message": {
        "sender.lastName.firstName": [
            "sender.lastName.firstName is required."
        ],

can you please give some advice how should i validate that array ?? thanks

4
  • it's items.*.sender.firstName You have to put the * where the array is. Commented Jan 3, 2023 at 7:01
  • its not working and when i get $data the items index have been removed. Commented Jan 3, 2023 at 7:17
  • Well it should not be removed. You can try *.sender.firstName but i'm not sure if it will work, i think it needs a toplevel field Commented Jan 3, 2023 at 7:17
  • ok thanks it worked thats all fine :)) you can answer that i accept yours Commented Jan 3, 2023 at 7:32

1 Answer 1

1

it's items.*.sender.firstName. You have to put the * where the array is

You should not remove items from the data.

You can try *.sender.firstName but i'm not sure if it will work, i think it needs a toplevel field

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

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.