2

i generated a multiple upload form with the former generator tool from https://github.com/Anahkiasen/former.

{{ Former::files('file')->accept('image')->required(); }}

that results in

<input multiple="true" accept="image/*" required="true" id="file[]" type="file" name="file[]">

After I've submit the form Ive figured out that Input::hasFile('file') always returns false whilst Input:has('file') returns true.

What i've tried until now:

Log::info(Input::file('file')); <-- empty

foreach(Input::file('file') as $file) <-- Invalid argument supplied for foreach()
    Log::info("test"); 

if(Input::has('file')) 
{
    if(is_array(Input::get('file')))
        foreach ( Input::get('file') as $file) 
        {
            Log::info($file); <-- returns the filename
            $validator = Validator::make( array('file' => $file), array('file' => 'required|image'));
            if($validator->fails()) {
                ...
            }
        }
}   

Of course, the validator always fails cause Input::get('file') does not return a file object. How do I have to modify my code to catch the submited files?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Azeruel
  • 210
  • 3
  • 10
  • Are you opening the form with `'files' => true` ? – Kestutis Jul 15 '14 at 15:09
  • Ive never seen this option but i gave it a try, unfortunately it doesnt change the result – Azeruel Jul 15 '14 at 15:18
  • That option sets proper encryption type (`enctype='multipart/form-data'`) which is necessary if you're trying to upload files, more about this [here](http://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean). – Kestutis Jul 15 '14 at 15:40
  • Here's a post that might help but I'm not sure it answers your question: http://forumsarchive.laravel.io/viewtopic.php?id=2839 – Dave Morrissey Jul 15 '14 at 21:38

1 Answers1

3

Thanks for the help, the answer from Kestutis made it clear. The common way to define a file form in Laravel is

echo Form::open(array('url' => 'foo/bar', 'files' => true))

This Options sets the proper encryption type with enctype='multipart/form-data'.

With the laravel form builder "Former" you have to open the form with

Former::open_for_files()

After that u can validate the form in the common way.

if(Input::hasFile('files')) {
            Log::info(Input::File('files'));
            $rules = array(
                ...
            );

            if(!array(Input::File('files')))
                $rules["files"] = 'required|image';
            else 
                for($i=0;$i<count(Input::File('files'));$i++) {
                    $rules["files.$i"] = 'required|image';
                }

            $validation = Validator::make(Input::all(), $rules);

            if ($validation->fails()) 
            {
                return ...
            }
            else {
                // everything is ok ...
            }
Azeruel
  • 210
  • 3
  • 10