gpt4 book ai didi

php - Laravel 填充数组元素的验证规则

转载 作者:行者123 更新时间:2023-12-03 22:21:09 24 4
gpt4 key购买 nike

如果至少有一个数组元素为空,我需要检查输入的字符串数组并发出警告。

使用以下规则:

return Validator::make($data, [
'branches' => 'array',
'branches.*' => 'filled|max:255'
]);

但是似乎填充规则不起作用(而 min:1 工作正常)。
它应该与数组元素一起使用吗?

更新:
分支数组不是强制性的,但如果存在,它应该包含非空元素。

更新:
终于在我的验证规则中发现了错误。
它应该看起来像
return Validator::make($data, [
'branches' => 'array',
'branches.*.*' => 'filled|max:255'
]);

因为输入数组是数组数组。现在填充规则与我的输入数据按预期工作。

最佳答案

使用 required 代替

return Validator::make($data, [
'branches' => 'required|array',
'branches.*' => 'required|max:255'
]);

来自文档: https://laravel.com/docs/5.5/validation#available-validation-rules

required

The field under validation must be present in the input data and not empty. A field is considered "empty" if one of the following conditions are true:

  • The value is null.
  • The value is an empty string.
  • The value is an empty array or empty Countable object.
  • The value is an uploaded file with no path.


如果您只想在存在字段数据时验证数组,请使用 filled .您可以将此与 present 结合使用.
return Validator::make($data, [
'branches' => 'present|array',
'branches.*' => 'filled|max:255'
]);

filled

The field under validation must not be empty when it is present.

present

The field under validation must be present in the input data but can be empty.

关于php - Laravel 填充数组元素的验证规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47043570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com