Validation "OR" condition

2.9k views Asked by At

I want to be able to check if the file uploaded is either an image or a file with MIME type video/mp4 (mp4 video). However, I'm not sure how to do this as it would require an "OR" condition in the validation.

Right now, all I have is the image check:

$validator = Validator::make(array('fileUpload' => $fileUpload), [
    'fileUpload' => 'required|image',
]);

How can I add the "OR" condition to also check if the file has a MIME type of video/mp4?

2

There are 2 answers

2
mopo922 On

You can write your own validation rule to do this:

https://laravel.com/docs/5.3/validation#custom-validation-rules

Yours might look something like this:

Validator::extend('img_or_mp4', function ($attribute, $value, $parameters, $validator) {
    return X; // Check the data in $value to see if it's an image or mp4
});

Then your rule will be:

    'fileUpload' => 'required|img_or_mp4',
0
baikho On

You could also use the MIME Types rule or MIME rule :

$validator = Validator::make(
    ['fileUpload' => $fileUpload],
    ['fileUpload' => 'required|mimetypes:image/jpeg,image/bmp,image/png,video/mp4']
);