【发布时间】:2022-01-19 08:58:11
【问题描述】:
我有三个领域
- 标题
- 身体
- 图片[]
正文需要没有图像,所以我做了以下:
$request->validate([
'title' => 'required|string|max:' . (int) Setting::getValue('titlemaxchars'),
'body' => [
'required_without:images',
'string',
'min:'.(int) Setting::getValue('postminchars'),
'max:'.(int) Setting::getValue('postmaxchars'),
],
]);
if ($request->hasFile('images')) {
foreach ($request->file('images') as $file) {
Validator::validate(['photo' => $file], [
'photo' => ['required', 'file', 'image', 'max:2048'],
]);
}
}
问题在于:
- body.string
- body.min
- body.max
如果有图像,我不希望他们发出任何错误消息,并且只有在没有图像时才会出现错误消息。
【问题讨论】:
-
在正文中添加另一个验证字段
nullable/sometimes应该可以解决它 -
我添加了 nullable 但即使没有图像@FarhanIbnWahid 也会使请求成功
-
另一种猜测:
required_without:images将其更改为required_without:images.* -
我试过了,但是不行。
-
将字符串、最小值、最大值添加到正文中。当图像为空时
标签: laravel validation