Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What to return from validate function on client? #243

Open
greenwolfe opened this issue Jul 25, 2016 · 1 comment
Open

What to return from validate function on client? #243

greenwolfe opened this issue Jul 25, 2016 · 1 comment

Comments

@greenwolfe
Copy link

greenwolfe commented Jul 25, 2016

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
Copy link

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"}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants