You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 动态数组表单全索引校验自定义值翻译方法

动态数组表单项的校验值翻译实现

场景说明

我们实现了支持动态增删的数组结构表单,每个表单项可选择simple或complex两种类型,选中complex类型时需展示额外的“复杂信息”输入框,基础表单结构如下:

<div class="item">
    <div>
        <h2>Type</h2>
        <label><input type="radio" name="data[0][type]" value="simple"> Simple ABC stuff</label>
        <label><input type="radio" name="data[0][type]" value="complex"> Complex XYZ stuff</label>
    </div>
    <div class="complex"> <!-- 仅选中Complex类型时显示 -->
        <h2>Complex info</h2>
        <input type="text" name="data[0][complex_info]"/>
    </div>
    <button class="remove">Remove item</button>
</div>
<div class="item">
    <div>
        <h2>Type</h2>
        <label><input type="radio" name="data[1][type]" value="simple"> Simple ABC stuff</label>
        <label><input type="radio" name="data[1][type]" value="complex"> Complex XYZ stuff</label>
    </div>
    <div class="complex"> <!-- 仅选中Complex类型时显示 -->
        <h2>Complex info</h2>
        <input type="text" name="data[1][complex_info]"/>
    </div>
    <button class="remove">Remove item</button>
</div>
<button class="add">Add item</button>

校验规则配置

针对选中complex类型时“复杂信息”为必填的业务要求,在Laravel 6.x的表单请求类中定义校验规则:

public function rules() {
    return ['data.*.complex_info' => 'required_if:data.*.type,complex'];
}

校验失败时,Laravel默认的错误提示模板为The :attribute field is required when :other is :value.,预期填充后的提示文本为:The Complex info field is required when Type is Complex XYZ stuff.

现有方案的问题

针对固定数量的表单项,逐索引在validation.php的values配置中定义值映射,可正常实现翻译效果:

return [
    /* ... */
    'values' => [
        'data' => [
            0 => [
                'type' => [
                    'simple' => 'Simple ABC stuff',
                    'complex' => 'Complex XYZ stuff',
                ]
            ],
            1 => [
                'type' => [
                    'simple' => 'Simple ABC stuff',
                    'complex' => 'Complex XYZ stuff',
                ]
            ],
        ]
    ]
];

由于表单支持动态增删,表单项索引不固定,尝试使用*通配符覆盖所有索引,配置如下:

return [
    /* ... */
    'values' => [
        'data' => [
            '*' => [
                'type' => [
                    'simple' => 'Simple ABC stuff',
                    'complex' => 'Complex XYZ stuff',
                ]
            ],
        ]
    ]
];

该配置未生效,校验失败时:value参数仍输出原始值complex,最终提示为The Complex info field is required when Type is complex.

问题原因

Laravel 6.x 的校验值翻译查找逻辑,会沿着校验属性的完整路径逐段匹配语言文件配置。*通配符仅在校验规则定义、属性名匹配阶段生效,在values配置层级,系统不会对数组索引位置的路径做通配符回溯匹配。当实际校验属性路径为data.3.type这类动态路径时,系统会严格查找对应路径的配置,不会匹配data.*.type的通配配置,因此之前的写法无法生效。

实现方案

以下两种方式均可适配任意数量、任意索引的动态表单项:

  • 利用路径回溯配置通用值映射
    不需要为每个数组索引单独写配置,直接在data.type的通用层级定义枚举值映射。系统在查找data.任意索引.type的翻译值时,会自动向上回溯父级路径的配置完成匹配:
    return [
        /* ... */
        'values' => [
            'data' => [
                'type' => [
                    'simple' => 'Simple ABC stuff',
                    'complex' => 'Complex XYZ stuff',
                ]
            ]
        ],
        // 同步配置属性名的通用翻译
        'attributes' => [
            'data.*.complex_info' => 'Complex info',
            'data.*.type' => 'Type',
        ]
    ];
    
  • 自定义规则级别的错误提示
    在表单请求类的messages方法中,针对动态数组的校验规则直接定义带完整翻译值的错误提示,绕过默认模板的参数填充逻辑:
    public function messages()
    {
        return [
            'data.*.complex_info.required_if' => 'The Complex info field is required when Type is Complex XYZ stuff.',
        ];
    }
    

效果验证

完成配置后,无论动态新增多少个表单项、索引如何变化,校验失败时的错误提示都会正确展示翻译后的枚举值,不会再输出原始的complex值。


内容的提问来源于stack exchange,提问作者cawecoy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.25 07:06:10