我需要为其元素可以是字符串或包含字符串的后续数组的动态数组添加验证,但我找不到保存此规则的方法。
我当前的验证:
$data = $request->validate([
'answers' => ['required', 'array'],
'answers.*' => ['required', 'max:255'],
'answers.*.*' => ['nullable', 'string', 'max:255']
]);
输入示例:
"answers" => array:3 [▼
4 => array:1 [▼
0 => "Tests"
1 => "Tests 2"
]
5 => "Test"
6 => "Yes"
]
创建自定义规则类。例如:
NestedArrayWithStrings
然后检查:
if (is_array($item))
在 for 循环等中
像这样添加到验证:
'answers' => ['required', 'array', new NestedArrayWithStrings],