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.
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
@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.
Please or to participate in this conversation.