laravel数组验证 - 计算出数组的深度

时间:2018-03-03 15:20:25

标签: laravel laravel-5

我正在尝试验证可能具有任意数量级别的递归数据,例如

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

在这个例子中,我需要以下规则来确保在每个级别指定一个名称:

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

我如何根据进来的数据动态生成验证规则?

0 个答案:

没有答案