Search code examples
javascriptfine-uploader

Validate (custom) before adding file


I would like to make sure if the user is logged-in or not before anything is added to the uploader area (thumbnails).

it's easy enough checking stuff on the validate event callback, but I can't seem to figure out how to stop the item from being added to the DOM, even if I return false in my validation or submit events...the item just gets a class of qq-upload-fail... but I want to completly do NOTHING if the user isn't logged in...maybe the validation event handler is not the place to put this logic, but where else?


My initialization code (jQuery):

this.holderElm.fineUploader({
    template: 'qq-simple-thumbnails-template',
    // uploaderType: 'basic',
    request: {
        endpoint: '/image/upload_image/' + this.settings.uploadID
    },
    validation: {
        allowedExtensions : ['jpeg', 'jpg', 'gif', 'png'],
        sizeLimit         : this.settings.sizeLimit,
        itemLimit         : this.settings.itemsLimit
    },
    retry: {
        enableAuto: true
    }
})
.on("validate", this.onValidate.bind(this) )
.on('submit', this.onSubmit.bind(this) )
.on('error', this.onError.bind(this) )
.on('upload', this.onUpload.bind(this) )
.on('complete', this.onComplete.bind(this) )
.on('allComplete', this.onAllComplete.bind(this) );

Solution

  • Use the onSubmit event -- which called is before the item has been added to the DOM -- and return false or a qq.Promise that will be rejected to disable the addition of that item to the file list.

    var userLoggedIn = true;
    
    // snippet ...    
    
    onSubmit: function(id, name){
        return userLoggedIn;
    },
    

    or with promises:

    function isUserLoggedIn(username){
      var promise = new qq.Promise();
    
      $.get("/api/user/"+username, function(err, data){
          if (err) return promise.failure(err);
          return promise.success(data);
      });
    
      return promise;
    }
    
    // snippet ...  
    
    onSubmit: function(id, name){
        return isUserLoggedIn(username);
    },