在 Laravel 9 中,复杂的条件验证的实现,基于嵌套数组中的一系列值,确认嵌套数组外层的值是否验证通过

1、现在请求参数如下,只有当 config.item_logistics_feature.value.*.invert_select 全部等于 0 时,才允许 config.item_logistics_feature.and_select 等于 1 ,否则,只能够等于 0。如图1

图1

2、参考:https://learnku.com/docs/laravel/9.x/validation/12219#3d1baa 。那么条件如下:当 config.item_logistics_feature.value.*.invert_select 其中某一个值等于 1 时,config.item_logistics_feature.and_select 必须等于 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.value.*.invert_select 全部等于 0 时,才允许 config.item_logistics_feature.and_select 等于 1 。验证通过。符合预期。如图2

图2

4、当 config.item_logistics_feature.value.*.invert_select 其中某一个值等于 1 时,config.item_logistics_feature.and_select 等于 1。验证失败。符合预期。如图3

图3

5、当 config.item_logistics_feature.value.*.invert_select 其中某一个值等于 1 时,config.item_logistics_feature.and_select 等于 0。验证通过。符合预期。如图4

图4

6、验证失败时,需要自定义消息。符合预期。如图5

图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、当请求参数 config[‘item_logistics_feature’] 不存在时,报错:Undefined array key “item_logistics_feature”。完善后的代码,当不存在时,赋值为空数组。

$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;
});
永夜