【发布时间】:2021-09-30 01:29:57
【问题描述】:
我正在尝试验证自定义请求中的数组。如果满足两个条件,则该规则评估为 required:
- 属性3是
true - 同一数组中的另一列是
true
这就是我正在做的事情:
public function rules()
{
return [
'attribute1' => 'required',
'attribute2' => 'required',
'attribute3' => 'required',
...
'attribute10.*.column3' => Rule::requiredIf(fn() => $this->attribute3), // <- array
'attribute10.*.column4' => Rule::requiredIf(fn() => $this->attribute3), // <- array
'attribute10.*.column5' => Rule::requiredIf(fn() => $this->attribute3), // <- array
];
}
我真正需要的是这个:
'attribute10.*.column4' => Rule::requiredIf(fn($item <- magically hint this currently looped item) => $this->attribute3 && $item->column2 <- so I can use it like this), // <- array
【问题讨论】:
-
你能分享一个传入请求的例子吗?您可以在返回之前在 FormRequest 的规则函数中执行
Log::debug($this->all());,然后检查 laravel 日志以查看请求中到达的内容。 -
@porloscerrosΨ - 我不想分享,因为这是公司的隐私,而且代码不是开源的。但信息是相同的 - 我收到一个数组,当根据规则检查时,我只想访问它的当前项目。
-
好的,我发布了一个假设请求结构的答案。也许你必须适应它,但你有一个想法
-
前段时间我回答了另一个问题,它不一样,但你可以根据你的要求调整它的想法stackoverflow.com/a/59198620/7498116
标签: laravel validation