tomitrescak / meteor-uploads

MIT License
295 stars 41 forks source link

What to return from validate function on client? #243

Open greenwolfe opened 8 years ago

greenwolfe commented 8 years ago

I have this validate function on the client:

<p>
  {{>upload_bootstrap multiple=false formData=formData callbacks=processUpload}}
 </p> 
    processUpload: function() { 
      return {
        finished: function(index, file, tmpl) {
          //calls meteor method
        },
        validate: function(files) {
          return (_.max(_.pluck(files,'size')) < 1e8); //100MB
       }
     }
    }

With this line in the return, the file is validated and the upload is successful. I would like to test some other things in the validate function, such as file type, creating an alert and causing the validation to fail if the files are of the wrong type. Testing for file type and sending the alert is no problem, but if I try to return any boolean, or any other value from the validate function, validation simply fails. In fact, so far as I have found, only the line above is successful. It may not even be testing successfully for file size. I have read the documentation several times, but am confused by the two forms of validate function on the server versus the validate function on the client and still do not have a clear idea of what to return.

Matt

bluefangs commented 4 years ago

Three years later, I was in a similar situation. This worked for me. I'm just leaving it here for future references.

Helpers:

Template.updateCenter.helpers({
    customCallback: function() {

        return {
            // formData: function() { return { id: "232323", other: Session.get("ReactiveParam") } },
            finished: function(index, fileInfo, context) {

                if(!fileInfo.error){
                    console.log('the upload is successful'); // success toast message here
                }

            },
            validate: function(files) {

                if (!(files[0].name.endsWith('.wow') || files[0].name.endsWith('.WOW'))) {
                   console.log("Not a wow file"); // error toast message here
                    return null
                }

                if (files[0].size > 1e9) { // 1GB
                   console.log("file size > 1gb"); // error toast message here
                    return null;
                }

                return files;
            }

        }
    }
});

HTML Template

{{> upload_bootstrap fileTypes='.wow, .WOW' multiple=false callbacks=customCallback placeholder="update-YYYYMMDD.wow"}}