A
A
Andrey Boychenko2016-05-14 08:50:33
JavaScript
Andrey Boychenko, 2016-05-14 08:50:33

Laravel5 + dropzone js files not validating?

Good afternoon! I use the dropzone.js library to upload files and everything works with a bang if you do not use laravel validation for uploaded files. Below is the validation code and the download method

public function upload(UplloadNewGalleryFiles $request)
    {
        $input = $request->all();
        $gallery = new gallery();
        $title = $input['title'];
        $directoryName = parent::randomName(24);
        $disk = Storage::disk('gallery');

        if($disk->makeDirectory($directoryName))
        {
            $gallery->title = $title;
            $gallery->folder = "images/gallery/$directoryName";
            $gallery->created_at = Carbon::now();
            $gallery->save();
        }

        foreach( $input['file'] as $file )
        {
            $ext = $file->getClientOriginalExtension();
            $name = parent::randomName(24) . ".$ext";
            $file->move( "images/gallery/$directoryName" , $name);

            $galleryFile = new files();
            $galleryFile->gallery = $title;
            $galleryFile->name = $name;
            $galleryFile->extention = $ext;
            $galleryFile->created_at = Carbon::now();
            $galleryFile->save();
        }
    }

 public function rules()
    {
        return [
            'title' => 'unique:gallery,title',
            'file' => 'required|mimes:jpeg,bmp,png',
            //
        ];
    }

{"file":["The file must be a file of type: jpeg, bmp, png."]}

All downloaded files have a jpeg extension, where is the catch? Thank you very much!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrzej Wielski, 2016-05-14
@Ka4_Piton

There is a magic "asterisk" for working with arrays in validation.
In your case check each file in $request->file array -

$this->validate($request, [
   'file.*' => 'required|mimes:jpeg,bmp,png'
]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question