lundi 2 mai 2016

Laravel validation not working with array of files

I am submitting an array of file fields to Laravel and I want to run validation on each file to check that it is the correct mime type (only PNG, JPG and GIF should be allowed) However if I upload a TIF image file the validation passes. Here is what I am submitting:

Request URL:http://ift.tt/1W48Uvy
Request Method:POST
Status Code:200 OK
Remote Address:127.0.0.1:80
Response Headers
view source
Connection:Keep-Alive
Content-Length:6
Content-Type:text/html; charset=UTF-8
Date:Mon, 02 May 2016 19:05:18 GMT
Keep-Alive:timeout=5, max=99
Server:Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.6.3
X-Powered-By:PHP/5.6.3
Request Headers
view source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:19912006
Content-Type:multipart/form-data; boundary=----WebKitFormBoundarypWGUvcecHBZlzNcP
Cookie:__utma=252126427.1839499577.1452033482.1452033483.1452072866.2; __utmz=252126427.1452033483.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _gat=1; _ga=GA1.2.1839499577.1452033482; laravel_session=eyJpdiI6InVxazdLSFwvNWNRUnl2eDQrRmFhdUpBPT0iLCJ2YWx1ZSI6IlJRXC9mRk9EYXBUWUpQMHpFMms3Mm9hYkRiekNjUlJHTDlBS0hpYkk2R091RmFYQzVsM1JWcEdMdmFzRjl3Q3BoaEZHQUM3VFc5ZE5Oak5KdHowaU0zdz09IiwibWFjIjoiZTYxM2U3MmVhNzU4NmFiZDNmYWFhNjdiZDE3NDczM2IxN2RmYWFhYWZlMzhiNTBiM2IwZjEyYWQwNThhMTk1MyJ9
Host:local.website.com
Origin:http://ift.tt/1relTgN
Pragma:no-cache
Referer:http://ift.tt/1relTgN
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payload
------WebKitFormBoundarypWGUvcecHBZlzNcP
Content-Disposition: form-data; name="files[]"; filename="eye design.png"
Content-Type: image/png


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


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


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


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


------WebKitFormBoundarypWGUvcecHBZlzNcP--

Here is are the contents of my request:

<?php namespace App\Http\Requests;

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

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

    public function messages()
    {
        $messages = [
            'files.required' => 'You must upload a file.'
        ];

        foreach($this->files as $key => $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;
    }
}

This is my route for the form being submitted:

Route::post('upload', ['uses' => 'UploadController@index', 'as' => 'upload.post']);

This is my UploadController file:

<?php namespace App\Http\Controllers;

use Response;
use Illuminate\Http\Request;
use Input;
use Image;
use Session;
use App\Http\Requests\UploadRequest;
use Auth;
use App\Setting;
use App\Discount;
use App\User_credit;
use App\Customer_group_discount;
use Event;
use App\Events\ImagesUpdated;

class UploadController extends Controller {

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * Handles the uploading of images
     *
     * @return Response json
     */
    public function index(UploadRequest $request)
    {
        // validation has passed do stuff
    }
}

The request is definitely being run but it just isn't failing when it should do. What am I doing wrong?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire