samedi 30 avril 2016

Laravel 5.1 validating array of files now working

I am trying to validate an array of files to check that they are the right mime type but it's not working I get the following error:

Undefined index: files.0

I get what this error is trying to tell me but I don't understand why it is happening. Here is what I am submitting:

------WebKitFormBoundary8wDAKRGDzd5kLrTO
Content-Disposition: form-data; name="files[]"; filename="eye design.png"
Content-Type: image/png


------WebKitFormBoundary8wDAKRGDzd5kLrTO
Content-Disposition: form-data; name="files[]"; filename="hd flamingo.tif"
Content-Type: image/tiff


------WebKitFormBoundary8wDAKRGDzd5kLrTO
Content-Disposition: form-data; name="files[]"; filename="Picture 3.tif"
Content-Type: image/tiff


------WebKitFormBoundary8wDAKRGDzd5kLrTO
Content-Disposition: form-data; name="files[]"; filename="Picture 16.tif"
Content-Type: image/tiff


------WebKitFormBoundary8wDAKRGDzd5kLrTO
Content-Disposition: form-data; name="files[]"; filename="Picture 23.tif"
Content-Type: image/tiff


------WebKitFormBoundary8wDAKRGDzd5kLrTO--

Here is my request:

<?php namespace App\Http\Requests;

use Response;
use Illuminate\Foundation\Http\FormRequest;

class UploadRequest extends FormRequest
{
    public function rules()
    {
        $rules = [];
        foreach($this->files as $key => $file) {
            $rules['files.'.$key] = 'required|image|mimes:jpeg,png,gif';
        }
        return $rules;
    }

    public function messages()
    {
        $messages = [];

        foreach($this->files as $key => $file) {
            $messages['files.'.$key.'.required'] = 'You must upload a file.';
            $messages['files.'.$key.'.image'] = 'The upload file must be an image.';
            $messages['files.'.$key.'.mimes'] = 'The image must be one of the following types: JPEG, PNG, or GIF.';
        }
        return $messages;
    }

    public function authorize()
    {
        return true;
    }

}

And inside the rules method of my request if I do print_r($this->files) this is what I get:

Symfony\Component\HttpFoundation\FileBag Object
(
    [parameters:protected] => Array
        (
            [files] => Array
                (
                    [0] => Symfony\Component\HttpFoundation\File\UploadedFile Object
                        (
                            [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 
                            [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => eye design.png
                            [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/png
                            [size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 812968
                            [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
                            [pathName:SplFileInfo:private] => C:\xampp\tmp\php4898.tmp
                            [fileName:SplFileInfo:private] => php4898.tmp
                        )

                    [1] => Symfony\Component\HttpFoundation\File\UploadedFile Object
                        (
                            [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 
                            [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => hd flamingo.tif
                            [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/tiff
                            [size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 4579592
                            [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
                            [pathName:SplFileInfo:private] => C:\xampp\tmp\php48A9.tmp
                            [fileName:SplFileInfo:private] => php48A9.tmp
                        )

                    [2] => Symfony\Component\HttpFoundation\File\UploadedFile Object
                        (
                            [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 
                            [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => Picture 3.tif
                            [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/tiff
                            [size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 4158508
                            [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
                            [pathName:SplFileInfo:private] => C:\xampp\tmp\php48B9.tmp
                            [fileName:SplFileInfo:private] => php48B9.tmp
                        )

                    [3] => Symfony\Component\HttpFoundation\File\UploadedFile Object
                        (
                            [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 
                            [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => Picture 16.tif
                            [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/tiff
                            [size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 6080280
                            [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
                            [pathName:SplFileInfo:private] => C:\xampp\tmp\php48DA.tmp
                            [fileName:SplFileInfo:private] => php48DA.tmp
                        )

                    [4] => Symfony\Component\HttpFoundation\File\UploadedFile Object
                        (
                            [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 
                            [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => Picture 23.tif
                            [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/tiff
                            [size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 4279880
                            [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
                            [pathName:SplFileInfo:private] => C:\xampp\tmp\php48EA.tmp
                            [fileName:SplFileInfo:private] => php48EA.tmp
                        )

                )

        )

)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire