Validate the implementation of the index array in Laravel 9
1. The current request parameter structure is as follows. Prepare to verify config[‘appoint_country’][‘appoint_country’][‘value’][0], config[‘appoint_country’][‘appoint_country’][‘value’][1]. as shown in Figure 1
{
"title": "自动交运",
"level": 1,
"logistics_channel_id": 474241,
"auto_shipment": 1,
"description": null,
"is_publish": 1,
"config": {
"appoint_country": {
"invert_select": 0,
"value": [
"AD",
"AE"
]
},
"reship_order": {
"value": 0
}
}
}
2. The verification code is implemented as follows. Try to verify that the maximum length of the value in the index array is 1. Validation failed, as expected. as shown in Figure 2
$validator = Validator::make(
$request->all(),
[
'config.appoint_country' => 'array',
'config.appoint_country.invert_select' => 'required_with:config.appoint_country|in:0,1',
'config.appoint_country.value' => 'required_with:config.appoint_country|array',
'config.appoint_country.value.*' => 'required_with:config.appoint_country.value.*|max:1',
],
[
'config.appoint_country.array' => '订单目的地为指定国家参数类型错误',
'config.appoint_country.invert_select.required_with' => '是否反选参数为必填',
'config.appoint_country.invert_select.in' => '是否反选参数类型错误',
'config.appoint_country.value.required_with' => '请选择国家',
'config.appoint_country.value.array' => '国家参数类型错误',
]
);
3. Specify a custom message for a given attribute. adjustment as follows
$validator = Validator::make(
$request->all(),
[
'config.appoint_country' => 'array',
'config.appoint_country.invert_select' => 'required_with:config.appoint_country|in:0,1',
'config.appoint_country.value' => 'required_with:config.appoint_country|array',
'config.appoint_country.value.*' => 'required_with:config.appoint_country.value.*|max:1',
],
[
'config.appoint_country.array' => '订单目的地为指定国家参数类型错误',
'config.appoint_country.invert_select.required_with' => '是否反选参数为必填',
'config.appoint_country.invert_select.in' => '是否反选参数类型错误',
'config.appoint_country.value.required_with' => '请选择国家',
'config.appoint_country.value.array' => '国家参数类型错误',
'config.appoint_country.value.*.max' => '国家参数类型最大长度是1',
]
);
4. After the verification fails, respond to the custom message. as shown in Figure 3
![现在的请求参数结构如下所示。准备验证 config['appoint_country']['appoint_country']['value'][0]、config['appoint_country']['appoint_country']['value'][1]](https://www.shuijingwanwq.com/wp-content/uploads/2024/03/1-5.png)

