Validating multiple file uploads with Laravel 4
laravel, php
Solution
Well, I'm not exactly sure what was causing the error, but after playing around a bit with the validator, I figured out that I couldn't just pass all the files at once. Instead, I made a new array that associated each file with keys 'file0', 'file1', etc. Then I passed these to the validator and set rules for each one (using a foreach loop). Then the validator worked as expected. Still, it wasn't flexible enough for my needs and I ended up using a non-laravel solution.
Problem
How do I go about validating an array of uploaded files in Laravel 4? I've set it in the form to allow multiple files, and I've tested that the files exist in the Input::file('files') array. But how do I validate each file? Here's what I've tried: ``` $notesData = array( 'date' => Input::get('date'), 'files' => Input::file('files') ); // Declare the rules for the form validation. $rules = array( 'date' => 'Required|date_format:Y-m-d', 'files' => 'mimes:jpeg,bmp,png,pdf,doc' ); // Validate the inputs. $validator = Validator::make($notesData, $rules); // Check if the form validates with success. if ($validator->passes()) { // Redirect to homepage return Redirect::to('')->with('success', 'Validation passed!'); } // Something went wrong. return Redirect::to(URL::previous())->withErrors($validator)->withInput(Input::all()); ``` I expected Validator to complain about passing an array of files inside the data array, but it just passed validation even though the file I sent it was an mp3. When I tried to upload multiple files, it gave an unrelated error that the date field is required (though the date field was autopopulated). I'm pretty new to Laravel. What could I do to get this working? UPDATE: I figured out part of the problem was my upload_max_filesize and post_max_size, which I fixed. I've also tried dynamically adding the files to the arrays as so: ``` $notesData = array( 'date' => Input::get('date') ); $i=0; foreach(\Input::file('files') as $file){ $notesData['file'.++$i] = $file; } // Declare the rules for the form validation. $rules = array( 'date' => 'Required|date_format:Y-m-d' ); for($j=1; $j<=$i; $j++){ $rules['file'.$j] ='mimes:jpeg,bmp,png,doc'; } ``` But now I'm getting the following error: Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed And I'm lost. Any idea how to fix this?