1
{
     "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'
4
  • Manually iterate until there is node.children Commented Feb 15, 2021 at 4:41
  • 1
    how can i iterate? i tried it many times Commented Feb 15, 2021 at 4:43
  • I mean by overriding the validation process. But the simplest thing to do would be to check the rules manually on form submit Commented Feb 15, 2021 at 4:48
  • 1
    i am not using form validation Commented Feb 15, 2021 at 5:10

1 Answer 1

1

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;
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I am using this to store nested array in DB its working but I am trying to use it on validation rules but i can't. $node[] = $request->all(); //loop for each nested array foreach($node as $node){ //create every nested array Node::create($node); }
yes @TheKayi'sKitchen, you can loop your result like that but probably have to change variable name a bit. foreach($nodes as $node)
validation rule does not support recursive validation like this afaik.
$rules[] = ([ 'name' => 'required|string', 'children' => 'array', 'children.*.name' => 'required|string', ]); i tried it like this but it only validates first child only
@TheKayi'sKitchen yes, that's what i was saying

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.