validation - Required_with 验证数组
问题描述
使用 required_with 验证验证数组时遇到问题。即使我什么也没输入,它总是显示。从两天前开始我就收到了这个错误。我想检查一个字段是否插入值,另一个字段必须插入值。如果没有插入,则另一个字段不需要插入。请问你能帮帮我吗?
这是我得到的错误,即使我输入值与否。有什么解决方案吗? 错误信息图片
验证请求代码。
'work_procedure[]'=>'nullable|required_with:times,work_points',
'times[]'=>'nullable|required_with:work_procedure',
'work_points[]'=>'nullable|required_with:work_procedure',
这是 Model.php
protected $fillable=[
'work_procedure',
'times',
'work_points',
];
解决方案
https://laravel.com/docs/8.x/validation#rule-required-with-all
required_with_all:foo,bar,...
仅当所有其他指定字段都存在且不为空时,验证中的字段必须存在且不为空。
required_without:foo,bar,...
仅当任何其他指定字段为空或不存在时,验证字段必须存在且不为空。
required_without_all:foo,bar,...
仅当所有其他指定字段为空或不存在时,验证字段必须存在且不为空。
例子
$request->validate([
'name' => 'required',
'surname' => 'required_with:name'
]);
$request->validate([
'name' => 'required',
'detail' => 'required|required_with:name',
]);
Validator::make($request->all(), [
'role_id' => Rule::required_with_all:foo,bar,
]);
推荐阅读
- node.js - NodeJS - 在 Promises-Chain 中从抛出错误的 try-catch 中恢复(异步 - “第二条路径”)
- dart - 错误:不要从另一个包中导入实现文件
- angular - 从前端角度隐藏 URL
- excel - 使用 Excel VBA 设置数据透视表过滤器的问题
- typescript - 如何获取 src 文件夹中的 .graphql 文件并将它们捆绑到一个 schema.graphql 文件到我的构建文件夹
- android - HttpsURLConnection 内存泄漏
- c++ - cin >> int 给定一个字符串将 int 分配给 0
- java - 无法在 FTP 服务器上写入 Base64 图像数据
- ag-grid - 如何从 CDN 加载 ag-grid-react
- html - div下的div在两列布局中