laravel - Laravel 验证:required_with 或 required_with_all 条件总是通过
问题描述
在这里发表了类似的帖子,但在我的情况下仍然不起作用。
$v = \Validator::make($keys, [
'overall' => 'required',
'taste' => 'sometimes|required_with_all:freshness, quantity, value',
'freshness' => 'sometimes|required_with_all:taste, quantity, value',
'quantity' => 'sometimes|required_with_all:taste, freshness, value',
'value' => 'sometimes|required_with_all: taste, quantity, freshness'
]);
dd($v->fails()); //-> false should be true
我试图总结我在做什么:
- 我有一个针对不同类型报价的评分系统
- “整体”始终是必需的,无论它是哪种报价类型。
- 像食物这样的报价具有“整体、味道、新鲜度、数量和价值”的评分类型
- 但是,由于有许多报价类型,“味道、新鲜度、数量和价值”并不是唯一的评级类型。将来会有更多(例如电子书的评分(您不会评价味道等))
- “味道、新鲜度、数量和价值”只是可选的,但如果只有一个,那么一切都应该是必需的。
我试图用“required_with”来做,但它不起作用。
我的示例数组是:
array:4 [
"overall" => 0
"freshness" => 1
"quantity" => 2
"value" => 3
]
缺少“味道”。
那么我做错了什么?
如果我完全理解文档:
required_with_all:foo,bar,...只有在所有其他指定字段都存在时,才必须存在正在验证的字段。
这意味着如果:
例如 'taste' => 'sometimes|required_with_all:freshness, quantity, value',表示当新鲜度、数量、值存在,而参数 'taste' 不存在时,就会发生验证错误。
但它不...
解决方案
有时:字段只有在数据数组中存在时才会被验证。
由于taste
未在您的示例数组中显示,因此无法验证。试试不sometimes
'taste' => 'required_with_all:freshness, quantity, value'
推荐阅读
- python - Django TemplateView 上下文未在模板上呈现
- node.js - Loopback4:在开发和生产环境之间切换
- c++ - 无法用多重继承c ++实例化抽象类
- c# - C# 将 DateTime.Now 转换为带有阿拉伯数字的阿拉伯月份
- assembly - 如何以行优先顺序正确地将 MIPS 程序集中的两个 4x4 数组相乘?
- google-bigquery - BigQuery 会将记录移动到正确的分区中吗?
- angular - Angular - 重复相同的订阅 HTTP 请求
- mysql - MySQL - 基于另一个字段仅选择一次值
- python - 多个 Bokeh 服务器集成到 Flask
- android - react-native NFC 阅读器仅在手机锁定时启用