0

I hope you guys can help me.

I have these file inputs in a form:

Select images: <input type="file" name="images[][image]" multiple>
Select videos: <input type="file" name="videos[][video]" multiple>

And my goal is to force the user to upload an image if there is no video introduced. Possibilities:

  • At least one image
  • At least one video
  • Any combination of images with videos

What I want to avoid:

  • No images and no videos

But I'm confused about the validation I need to use in my controller. This is what I have for the moment:

$this->validate($request, [
    'images' => 'required_without_all:videos.*.video',
    'images.*.image' => 'image',
    'videos.*.video' => 'mimetypes:video/avi,video/mpeg,video/quicktime,video/mp4'
]);

But it is not working. I also tried (without success):

$this->validate($request, [
    'images.*.image' => 'image|required_without_all:videos.*.video',
    'videos.*.video' => 'mimetypes:video/avi,video/mpeg,video/quicktime,video/mp4'
]);

How can I achieve what I want using Laravel's validation?

Thank you very much in advanced.

Erick Ramírez
  • 75
  • 2
  • 14

2 Answers2

0

You have 2 dimensional array of input. required_without_all is the validation rule for existence of one field when all other are not present. In your case, you have two input fields each with multiple file upload.

So, What you need to do is toggle your cases using required_without validation. Try this one, May help you.

$this->validate($request,[
    'images.*.*' => 'required_without:videos.*.*',
    'videos.*.*' => 'required_without:images.*.*',
]);

This will check existence of image when no image is uploaded and in same way, will check existence of video when no image is uploaded.

Sagar Gautam
  • 9,049
  • 6
  • 53
  • 84
0

I ended up doing this to solve the problem:

$this->validate($request, [
    'images' => 'required_without_all:videos',
    'images.*.image' => 'image',
    'videos.*.video' => 'mimetypes:video/avi,video/mpeg,video/quicktime,video/mp4'
]);
Erick Ramírez
  • 75
  • 2
  • 14