没有不值得去解决的问题,也没有不值得去学习的技术!

Implementing Complex Conditional Validation in Laravel 9 Based on Nested Array Values

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

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.

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
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.

PHP

$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.

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
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.

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
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.

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
Figure 4

6. When validation fails, a custom message is required. This matches expectations. See Figure 5.

When validation fails, a custom message is required. This matches expectations
Figure 5
PHP

$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.

PHP

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

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系