1. The request parameters are as follows. config.item_logistics_feature.and_select is allowed to equal 1 only when all values of config.item_logistics_feature.value.*.invert_select equal 0; otherwise, it must equal 0. See Figure 1.

2. Reference: https://learnku.com/docs/laravel/9.x/validation/12219#3d1baa . The condition is therefore as follows: when any value in config.item_logistics_feature.value.*.invert_select equals 1, config.item_logistics_feature.and_select must equal 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. config.item_logistics_feature.and_select is allowed to equal 1 only when all values of config.item_logistics_feature.value.*.invert_select equal 0. Validation passes. This matches expectations. See Figure 2.

4. When any value in config.item_logistics_feature.value.*.invert_select equals 1, config.item_logistics_feature.and_select equals 1. Validation fails. This matches expectations. See Figure 3.

5. When any value in config.item_logistics_feature.value.*.invert_select equals 1, config.item_logistics_feature.and_select equals 0. Validation passes. This matches expectations. See Figure 4.

6. When validation fails, a custom message is required. This matches expectations. See 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 config[‘item_logistics_feature’] does not exist, an error occurs: Undefined array key “item_logistics_feature”. The improved code assigns an empty array when it does not exist.
$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;
});
