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

在 Laravel 9 中,验证索引数组的实现

验证失败后,响应自定义的消息
1、现在的请求参数结构如下所示。准备验证 config[‘appoint_country’][‘appoint_country’][‘value’][0]、config[‘appoint_country’][‘appoint_country’][‘value’][1]。 如图1
现在的请求参数结构如下所示。准备验证 config['appoint_country']['appoint_country']['value'][0]、config['appoint_country']['appoint_country']['value'][1]
图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、验证代码实现如下,尝试验证索引数组中的值的最大长度为 1。验证失败,符合预期。如图2
验证代码实现如下,尝试验证索引数组中的值的最大长度为 1。验证失败,符合预期
图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、为给定属性指定自定义消息。调整如下


        $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、验证失败后,响应自定义的消息。如图3
验证失败后,响应自定义的消息
图3

PHP / Laravel / Yii2 老项目维护与长期技术支持

如果你的 PHP / Laravel / Yii2 项目已经上线,但遇到原开发离职、Bug 长期无人修复、接口不稳定、性能下降、代码难以接手等问题,可以联系我做一次远程技术排查。

适合以下情况:
✅ 老旧 PHP 系统无人维护
✅ Laravel / Yii2 项目 Bug 修复
✅ 后台管理系统小功能迭代
✅ RESTful API 接口排查
✅ MySQL / Redis / Nginx 性能问题
✅ 长期远程兼职维护

可先从一次小问题开始:
✅ 线上报错排查
✅ 接口异常分析
✅ 慢查询与性能瓶颈定位
✅ 代码结构初步评估
✅ 部署环境与日志检查

如需咨询,请联系我,并注明:PHP 维护咨询

联系方式:
Telegram:@shuijingwan
微信:13980074657
邮箱:shuijingwanwq@gmail.com

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理