In Laravel 9, the implementation of complex conditional validation, based on a series of values in the nested array, confirms whether the value of the outer layer of the nested array is verified
1. Now the request parameters are as follows, only if config.item_logistics_feature.value.*.invert_select is all equal to 0, is it allowed config.item_logistics_feature.and_select is equal to 1 , otherwise, it can only be equal to 0. as shown in Figure 1
2. Reference:https://learnku.com/docs/laravel/9.x/validation/12219#3d1baa. Then the conditions are as follows: when config.item_logistics_feature.value.*.invert_select is one of the values equal to 1 config.item_logistics_feature.and_select must be equal to 0.
$validator->sometimes('config.item_logistics_feature.and_select', 'integer|size:0', function ($input) {
$return = false;
foreach ($input->config['item_logistics_feature']['value'] as $value) {
$return = $value['invert_select'] === 1;
}
return $return;
});
3. Only allow if config.item_logistics_feature.value.*.invert_select is all equal to 0 config.item_logistics_feature.and_select is equal to 1 . Verification passed. in line with expectations. as shown in Figure 2
4. When config.item_logistics_feature.value.*.invert_select one of the values is equal to 1 config.item_logistics_feature.and_select is equal to 1. Validation failed. in line with expectations. as shown in Figure 3
5. When config.item_logistics_feature.value.*.invert_select is a value equal to 1 config.item_logistics_feature.and_select is equal to 0. Verification passed. in line with expectations. as shown in Figure 4
6. When the verification fails, you need to customize the message. in line with expectations. as shown in Figure 5
$validator = Validator::make(
$request->all(),
[
'config.item_logistics_feature' => 'array',
'config.item_logistics_feature.and_select' => 'required_with:config.item_logistics_feature|in:0,1',
'config.item_logistics_feature.value' => 'required_with:config.item_logistics_feature|array',
'config.item_logistics_feature.value.*.invert_select' => 'required_with:config.item_logistics_feature.value|in:0,1',
'config.item_logistics_feature.value.*.logistics_feature_id' => 'required_with:config.item_logistics_feature.value|numeric',
],
[
'config.item_logistics_feature.array' => '订单指定商品物流属性',
'config.item_logistics_feature.and_select.required_with' => '是否同条件内选项关系为“且”为必填',
'config.item_logistics_feature.and_select.in' => '是否同条件内选项关系为“且”类型错误',
'config.item_logistics_feature.and_select.size' => '是否同条件内选项关系为“且”在物流属性存在反选时不可勾选',
'config.item_logistics_feature.value.required_with' => '请选择商品物流属性',
'config.item_logistics_feature.value.array' => '商品物流属性参数类型错误',
// 'config.item_logistics_feature.invert_select.required_with' => '是否反选参数为必填',
// 'config.item_logistics_feature.invert_select.in' => '是否反选参数类型错误',
'config.item_logistics_feature.value.*.invert_select.required_with' => '是否反选参数为必填',
'config.item_logistics_feature.value.*.invert_select.in' => '是否反选参数类型错误',
'config.item_logistics_feature.value.*.logistics_feature_id.required_with' => '请选择物流属性',
'config.item_logistics_feature.value.*.logistics_feature_id.numeric' => '物流属性参数类型错误',
]
);
7. When the request parameter is config[‘item_logistics_feature’]When it does not exist, an error is reported: undefined array key “item_logistics_feature”. The perfect code, when not present, is assigned as an empty array.
$validator->sometimes('config.item_logistics_feature.and_select', 'integer|size:0', function ($input) {
$return = false;
$values = $input->config['item_logistics_feature']['value'] ?? [];
foreach ($values as $value) {
if ($value['invert_select'] === 1) {
$return = true;
break;
}
}
return $return;
});




