使用自定义错误消息的 required_with 没有效果

PHP代码

Validator::make(
    [
        "title" => [1 => "1", 2 => null],
        "body" => [1 => null,2 => "<p>2</p>"],
    ],
    [
        "title.1" => "required_with:body.1|nullable|string|max:255",
        "body.1" => "required_with:title.1|nullable|string",
        "title.2" => "required_with:body.2|nullable|string|max:255",
        "body.2" => "required_with:title.2|nullable|string",
    ],
    [
        "title.1.required_with:body.1" => "The 中文 (简体) title is required when content 中文 (简体) is present.",
        "body.1.required_with:title.1" => "The 中文 (简体) content is required when title 中文 (简体) is present.",
        "title.2.required_with:body.2" => "The 中文 (繁體) title is required when content 中文 (繁體) is present.",
        "body.2.required_with:title.2" => "The 中文 (繁體) content is required when title 中文 (繁體) is present.",
    ]
);

验证结果得到

The body.1 field is required when title.1 is present.
The title.2 field is required when body.2 is present.

而不是

The 中文 (简体) content is required when title 中文 (简体) is present.
The 中文 (繁體) title is required when content 中文 (繁體) is present.

然后我在 Illuminate\Validation\Concerns\FormatsMessagesgetFromLocalArray() 发现

"getFromLocalArray -> $keys"
array:2 [▼
  0 => "body.1.required_with"
  1 => "required_with"
]

"getFromLocalArray -> $this->customMessages"
array:4 [▼
  "title.1.required_with:body.1" => "The 中文 (简体) title is required when content 中文 (简体) is present."
  "body.1.required_with:title.1" => "The 中文 (简体) content is required when title 中文 (简体) is present."
  "title.2.required_with:body.2" => "The 中文 (繁體) title is required when content 中文 (繁體) is present."
  "body.2.required_with:title.2" => "The 中文 (繁體) content is required when title 中文 (繁體) is present."
]

keys 和 customMessages 的键名对不上,请问我的自定义验证规则有什么写错了吗?

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 2
Kurisu

改成这样试试...

 [
        "required_with:body.1" => "The 中文 (简体) title is required when content 中文 (简体) is present.",
        "required_with:title.1" => "The 中文 (简体) content is required when title 中文 (简体) is present.",
        "required_with:body.2" => "The 中文 (繁體) title is required when content 中文 (繁體) is present.",
        "required_with:title.2" => "The 中文 (繁體) content is required when title 中文 (繁體) is present.",
    ]

这样1 2的写...代码有点....

7年前 评论

@Kurisu 试了不行。感觉关键是和 "getFromLocalArray() -> $this->customMessages" 键名不对。对了是你 Laravel 5.4 吗?

7年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!