I have three Fields
- title
- body
- images[]
the body is required without image, so I did the following:
$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'],
]);
}
}
The issue is with the following:
- body.string
- body.min
- body.max
I don't want them to make any error message if there is an Image, and the error message should only appear if there are no images.
CodePudding user response:
THIS WORKS FOR ME!
$request->validate([
'title' => 'required|string|max:' . (int) Setting::getValue('titlemaxchars'),
]);
if(!empty($request->hasFile('images'))){
$request->validate([
'body' => 'nullable',
]);
}else{
$request->validate([
'body' => [
'required',
'string',
'min:'.(int) Setting::getValue('postminchars'),
'max:'.(int) Setting::getValue('postmaxchars'),
],
]);
}