Search code examples
phplaravellaravel-5.4laravel-validationlaravel-request

Laravel Request validation


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.


Solution

  • 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'
    ]);