There is no problem not worth solving, and no technology not worth learning!

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

现在的请求参数结构如下所示。准备验证 config['appoint_country']['appoint_country']['value'][0]、config['appoint_country']['appoint_country']['value'][1]
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

验证代码实现如下,尝试验证索引数组中的值的最大长度为 1。验证失败,符合预期
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

验证失败后,响应自定义的消息
Figure 3

PHP / Laravel / Yii2 Legacy Project Maintenance & Long-Term Technical Support

If your PHP / Laravel / Yii2 project is already in production but needs bug fixing, API troubleshooting, performance optimization, developer handover support, or long-term maintenance, feel free to contact me for remote technical support.

Ideal For:
✅ PHP legacy systems without active maintenance
✅ Laravel / Yii2 project bug fixes
✅ Admin panel feature iterations
✅ RESTful API troubleshooting
✅ MySQL / Redis / Nginx performance issues
✅ Long-term remote part-time maintenance

We can start with a small task:
✅ Production error troubleshooting
✅ API issue analysis
✅ Slow query and performance bottleneck diagnosis
✅ Initial code structure review
✅ Deployment environment and log inspection

If you would like to discuss your project, please contact me and mention: PHP Maintenance Consultation.

Contact Me:
Telegram: @shuijingwan
WeChat: 13980074657
Email: shuijingwanwq@gmail.com

评论

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.