【问题标题】:Laravel - How to translate validation values? Not possible?Laravel - 如何翻译验证值?不可能?
【发布时间】:2020-05-04 09:22:21
【问题描述】:

这是一个类似的问题和信息:
Laravel translate values required_if

这里是 GitHub 的功能要点,可以解决这个问题,它被合并但仍然无法工作: https://github.com/laravel/framework/pull/4037

这是我的验证规则

'input.deliver_to_address' => ['in:to_billing_address,to_other_address']
'input.delivery_company_name' => ['required_if:input.deliver_to_address,to_other_address'],

这是我的validation.php 文件:

'values' => [
        'to_other_address' => 'to other address',
        'input.deliver_to_address' => [
            'to_other_address' => 'to other address',
        ],
        'deliver_to_address' => [
            'to_other_address' => 'to other address',
        ],
    ],

如您所见,我已经尝试了所有可能的组合,但仍然显示验证规则:

The contact person field is required when deliver to address is to_other_address.

问题是,是否可以在 Laravel 中翻译数组验证值?

我使用的是 Laravel 6.2

【问题讨论】:

  • 这很有趣。深入研究源代码时,看起来一切都很好。但是,由于某种原因,返回的是键而不是值。
  • 好吧,我越深入我就越确信这是一个错误。听听这个功能是否真的对某人有用会很酷。

标签: php laravel


【解决方案1】:

已解决:

问题是我正在使用input.deliver_to_address,当Validator.php 尝试用翻译后的字符串替换值时,$key 将是validation.values.input.deliver_to_address.to_other_address

所以数组结构需要是这样的:

'values' => [
        'input' => [
            'deliver_to_address' => [
                'to_other_address' => 'to other address',
            ],
        ],
    ],

而不是

'values' => [
        'input.deliver_to_address' => [
            'to_other_address' => 'to other address',
        ],
    ],

因为属性名称中多了一个.input

【讨论】:

    猜你喜欢
    • 2022-08-05
    • 1970-01-01
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 2020-02-26
    • 2020-01-05
    相关资源
    最近更新 更多