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

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

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

6、验证失败时,需要自定义消息。符合预期。如图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;
});
PHP / Laravel / Yii2 老项目维护与长期技术支持
如果你的 PHP / Laravel / Yii2 项目已经上线,但遇到原开发离职、Bug 长期无人修复、接口不稳定、性能下降、代码难以接手等问题,可以联系我做一次远程技术排查。
适合以下情况:
✅ 老旧 PHP 系统无人维护
✅ Laravel / Yii2 项目 Bug 修复
✅ 后台管理系统小功能迭代
✅ RESTful API 接口排查
✅ MySQL / Redis / Nginx 性能问题
✅ 长期远程兼职维护
可先从一次小问题开始:
✅ 线上报错排查
✅ 接口异常分析
✅ 慢查询与性能瓶颈定位
✅ 代码结构初步评估
✅ 部署环境与日志检查
如需咨询,请联系我,并注明:PHP 维护咨询。
联系方式:
Telegram:@shuijingwan
微信:13980074657
邮箱:shuijingwanwq@gmail.com

发表回复