2

I am trying to validate recursive data which could have any number of levels e.g.

[
    'name' => 'test',
    'children' => [
        [
            'name' => 'test2'
        ],
        [
            'name' => 'test3',
            'children' => [
                'name' => 'test4'
            ]
        ],
        [
            'name' => 'test5',
            'children' => [
                'name' => 'test6'
                'children' => [
                    'name' => 'test7'
                ]
            ]
        ]
    ]
]

In this example I would require the following rules to ensure that a name is specified at each level:

$rules = [
    'name' => ['required'],
    'children' => ['array'],
    'children.*.name' => ['required'],
    'children.*.children' => ['array'],
    'children.*.children.*.name' => ['required'],
    'children.*.children.*.children' => ['array'],
    'children.*.children.*.children.*.name' => ['required'],
]

How could I dynamically generate the validation rules based on the data coming in?

the-a-train
  • 1,123
  • 13
  • 32
  • I have no idea if it's natively possible. However, What I would do is to make a [custom validator](https://stackoverflow.com/a/28425173/8068675) that will recursively test each nodes. – Clément Baconnier Mar 03 '18 at 16:03

0 Answers0