Home > Software engineering >  validated multiple nested array items in laravel
validated multiple nested array items in laravel

Time:01-03

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

CodePudding user response:

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

  • Related