samedi 3 mars 2018

laravel array validation - work out depth for array

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?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire