jeudi 11 février 2016

Dropzone.js and displaying request validation errors in Laravel 5.1

I'm using dropzone.js to upload a file. In my controller method I have the following validation set up:

$this->validate($request, ['logo' => 'image|mimes:jpg,jpeg,gif']);

If the validation fails It throws a 422 server response with the validation errors as json

{"logo":["The logo must be an image.","The logo must be a file of type: jpg, jpeg, gif."]}

With dropzone.js how can I parse error and insert into the following

tag:

@if ($errors->has('logo')) <p class="help-block" id="logo-error">{{ $errors->first('logo') }}</p> @endif

I have an errors event declared in my dropzone script but nothing seems to appear when i do a console.log:

         var baseUrl = "{{ url('/') }}";
         Dropzone.autoDiscover = false;

         $("#my-dropzone").dropzone({
             url: baseUrl + "/upload",
             paramName: "logo",
             uploadMultiple: false,
             maxFiles: 1,
             dictDefaultMessage: '',
             init: function() {
                 this.on("addedfile", function(file) {
                     console.log('addedfile...');
                     if (this.files[1]!=null){
                         this.removeFile(this.files[0]);
                     }
                 });
                 this.on("thumbnail", function(file, dataUrl) {
                     console.log('thumbnail...');
                     $('.dz-image-preview').hide();
                     $('.dz-file-preview').hide();
                 });
                 this.on("sending", function(file, xhr, formData) {
                     formData.append("_token", $('meta[name="csrf-token"]').attr('content'));
                 });
                 this.on("success", function(file, res) {
                     console.log('upload success...');
                     $('#img-thumb').attr('src', res.path);
                     $('input[name="logo"]').val(res.path);
                 });

             },
             error: function(file, response) {
                 console.log(response);
             }


         });



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire