3
votes

How can I use Laravel file validation to a multiple file inside a loop?

Usually in a single file validation, I can just do like

$validator = Validator::make($request->all(),[
    'image' => 'image|max_width:1000|max_height:1000'
]);
if($validator->fails()){
    //validation fails
}

but now I want to validate multiple files in a single process, like

//store to a variable
$file = $request->file('image');
if($file){ //if request file image exist
    foreach($request->file('image') as $i){
    //here, validate each file image, but it just I don't know how
    }
}

Now for the front end side, to elaborate about the process of sending multiple files in one post.

$(document).on("change",'input[type="file"]',function(){
    if($(this).val()!==""){
        var files = this.files;
        handleFileUpload(files);
        $(this).val("");
    }
});

function handleFileUpload(files)
{
    //create a formData
    var fd = new FormData();
    //loop through e
    for (var i = 0; i < files.length; i++) 
    {
        //append each file object to the formData
        fd.append('image[]', files[i]);
    }
    //this function is where ajax request will be process
    sendFileToServer(fd); 
}

and so in the back end if I dump the request, I get these

array(2) { [0]=> object(Illuminate\Http\UploadedFile)#214 (7) { ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> bool(false) ["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(50) "12400457_643171672489204_2452820305362127031_n.jpg" ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(10) "image/jpeg" ["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(18413) ["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(0) ["pathName":"SplFileInfo":private]=> string(25) "C:\wamp64\tmp\phpBAE4.tmp" ["fileName":"SplFileInfo":private]=> string(11) "phpBAE4.tmp" } [1]=> object(Illuminate\Http\UploadedFile)#217 (7) { ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> bool(false) ["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(39) "13296200_702501119889592_55691535_n.jpg" ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(10) "image/jpeg" ["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(95383) ["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(0) ["pathName":"SplFileInfo":private]=> string(25) "C:\wamp64\tmp\phpBAE5.tmp" ["fileName":"SplFileInfo":private]=> string(11) "phpBAE5.tmp" } }

any ideas, clues, help please on how to use Laravel file validation on a multiple files inside a loop?

PS: I'm running on Laravel 5.2

2

2 Answers

0
votes

You can try the following:

    foreach($this->input('image') as $key => $value){
        $rules['image.'.$key] = 'image|max_width:1000|max_height:1000';
    }
0
votes

Try sending the image file input field as an array and then using array validation. If you're using Laravel 5.2 you can use Array Validation and ditch the foreach loop for building the rules.

Firstly, you would need to set up your file input as an array...

If you're using one input field:

<input type="file" name="image[]" multiple>

Your request would then come through looking something like this:

array:2 [▼
  "_token" => "oX3sTXPDnNiVj9mX20VOHe2xo1Ahx6sWzU4VHZxh"
  "image" => array:2 [▼
    0 => UploadedFile {#147 ▶}
    1 => UploadedFile {#146 ▶}
  ]
]

Then using array validation your rules would be declared in the validator by utilising the asterix symbol to indicate an array field:

$validator = Validator::make($request->all(),[
    'image.*' => 'image|max_width:1000|max_height:1000'
]);

Check the docs here for more info: https://laravel.com/docs/5.2/validation#validating-arrays