Atif Mahmood
Atif Mahmood

Reputation: 390

Check validation on nested array dynamically

{
     "name" : "parent_node",
     "children: 
[
    {
        "name" : "child-1",
        "children:
    [
            {
                "name" : "subchild-1",
               //unlimited childrens

            }
     ]  
    }

    ]
}


               ------------------------------------------

$rules = ([
        'name' => 'required|string',
        'children.*.name' => 'required|string'
         ]);
  1. I want to check validation dynamically on dynamic nested array
  2. Instead of using 'children.*.name
  3. Validation should check nested array itself because i didn't bound nested array at any level, so there are infinite levels
  4. You can see the image 'https://drive.google.com/file/d/1PAZC_TQQpwSEsY-ecUjh4lKoNcpKoOj9/view?usp=sharing'

Upvotes: 1

Views: 405

Answers (1)

Anurat Chapanond
Anurat Chapanond

Reputation: 2987

I think it's not possible to validate recursively using Laravel validation. you will have to do it manually for example

$children = $request->children;
if(!is_null($children[0]) {
    $children[0]->name;

    if(!is_null($children[0]->children[0]) {
        $children[0]->children[0]->name;
    }
}

Upvotes: 1

Related Questions