我想为 laravel 中的对象数组设置需要验证并使用以下规则:
[
'translations.*.languageId' => ['required', 'numeric', Rule::in(Language::all()->pluck('id'))],
'translations.*.data.title' => 'required|string',
]
但是当我在没有 translations
的情况下发送请求时会出现问题 key validate
函数不会为 translation
抛出 require 错误 key 。所以我添加
translations
key 也分开。[
'translations' => ['required', 'array'],
'translations.*.languageId' => ['required', 'numeric', Rule::in(Language::all()->pluck('id'))],
'translations.*.data.title' => 'required|string',
]
但是如果发送了不应该在 translations
中的额外 key ,就会出现问题。数组(如 locale
),仍然可以在 validate
的输出中看到功能。我怎样才能防止这种意外的结果?
最佳答案
正如 taylorotwell 回答的那样 my issue并关闭它没有办法 validator
做这个过滤器。
Filter the array to only have the items you actually want after validation.
关于php - 如何在 Laravel 验证中要求数组并防止获得意外 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66349592/