Laravel:仅在存在另一个特定字段的情况下,才使字段为必填字段?

问题描述 投票:0回答:1

我有一个要求提供多个文件以及这些文件描述的表格。有点像

<input type="file" name="file1">
Describe your file:
<input type="text" name="desc1">

[我希望用户描述文件的内容,而不是只显示Invoices-final-FinalV30.docx之类的内容,他可能会说“ 2018年1月的发票”,因此,当我验证表单时,我知道如何询问字段是否跟随正则表达式,或者如果字段是必需的,等等,使用validate()方法,但是我想要自定义,只有在有“ file1”的情况下,才能使“ desc1”成为必需,如果没有“ file1”,我可以放心地忽略“ desc1”中的任何内容。

php laravel laravel-5.7
1个回答
3
投票

尝试required_with:otherfield验证

https://laravel.com/docs/5.7/validation

$validator = Validator::make(
$request->all(),
    [
    'file1' =>  'mimes:jpeg,bmp,png', //your file validation
    'desc1'  => 'bail|required_with:file1' //add other description validations
    ]
);

对于数组字段,示例名为upload[][file]upload[][desc]

$validator = Validator::make($request->all(), [
    'upload.*.file' => 'mimes:jpeg,bmp,png',
    'upload.*.desc' => 'bail|required_with:upload.*.file',
]);
© www.soinside.com 2019 - 2024. All rights reserved.