t0berius's avatar

laravel validation required_with not working as expected

Using the following validation ruleset:

        'productImage1'         => 'nullable|sometimes|mimes:jpeg,jpg,png|max:2000',
        'productImage2'         => 'nullable|sometimes|mimes:jpeg,jpg,png|max:2000|required_with:productImage1',
        'productImage3'         => 'nullable|sometimes|mimes:jpeg,jpg,png|max:2000|required_with:productImage2',
        'productImage4'         => 'nullable|sometimes|mimes:jpeg,jpg,png|max:2000|required_with:productImage3',

Using the validation rules I want to make sure, the images are only uploaded in the correct order. At the moment a user can choose for example only productImage3 and the validation will pass, any idea how to prevent this?

Seems like required_with:productImage2 is not working as expected for this scenario?

Validation should pass the following as valid:

-no upload detected (for all fields)

-uploads are in the correct order, meaning productImage1 can be filled, or productImage1 and productImage2 or productImage1,productImage2 and productImage3

0 likes
9 replies
Tray2's avatar

I think you should put the required_with first in the rules for the productImage2,3 and 4, then remove the nullable and sometimes rule for them.

t0berius's avatar

@tray2

You mean like this?

        'productImage1'         => 'nullable|sometimes|mimes:jpeg,jpg,png|max:2000',
        'productImage2'         => 'required_with:productImage1|mimes:jpeg,jpg,png|max:2000|',
        'productImage3'         => 'required_with:productImage2|mimes:jpeg,jpg,png|max:2000',
        'productImage4'         => 'required_with:productImage3|mimes:jpeg,jpg,png|max:2000',

This will not work too, you can still fill in 2,3 and 4 and the form will be validated when 1 is "empty" (meaning null).

jlrdw's avatar

Since you have an array of images, just use array functions and keys to determine if previous image key had an image. Something like:

            $files = Request::file('ufile');
            foreach ($files as $file) {
                if (empty($file)) {
                    break;
                } else {
                 // process away

t0berius's avatar

@jlrdw I don't use the array way anymore. This way here I decided to use.

jlrdw's avatar

Even if individual files you could do a check if the previous image had a file or not just work out the logic.

t0berius's avatar

@jlrdw I think you missunderstand the question a bit. The question is related to the validation itself, what you mean by previous image?

I want to do all logic inside the validation...

jlrdw's avatar

Yes still use validation but you may not be able to work out every little thing in validation.

And I mean after you verify there is image one for example you good to go for image 2 when you are on image 3 if there was no image 2 break.

The validation can still be used to make sure they are of correct type.

But you can check the type during the processing of the image as well in other words file has special validation built in.

Of course these are just suggestions.

t0berius's avatar

Any idea how to use the laravel validation functions for this?

jlrdw's avatar
jlrdw
Best Answer
Level 75

@t0berius You will probably want to work out a custom validation. I have to admit, the answers on the forum cover multiple uploads, but seldom ask about correct order, or leaving one out.

Another idea is collect the uploaded names, and if one is skipped, just move it up one, to me that would be the easiest way.

Basically change

null or '' (blank)
null
someimage
null

to

someimage
null
null
null

That's what I'd do. But again just a suggestion. But it's a good question that many folks don't think about.

Another thing to consider, does the placement matter, in the view you can check if an image exist:

            if (empty($row->pic2)) {
                echo "";
            } else {
                echo '<img src="' . DIR . 'upload/imgrecent/' . $row->pic2 . '" width="280" alt="" class="imgborder">';
                echo '<br><br>';
            }

Not blade, but maybe you get the idea, just store a blank for no image.

This is when the images are in same table, and up to 4 are allowed.

1 like

Please or to participate in this conversation.