JavaScript API — File validation

  • This documentation is for the version 3.2.3 of the widget. If you're looking for the JavaScript for Widget v2 docs, check here.

Validators allow restricting user choice to certain kinds of files. For example, you may want to only accept files less than 2MB in size or just PDF documents. With our API, a validator is a function that receives a fileInfo object for every uploaded file and throws an exception if that file does not meet your set requirements.

function imagesOnly(fileInfo) {
  if (fileInfo.isImage === false) {
    throw new Error('image');
  }
}

Validators can be called once the info on file becomes available. All unknown fields used for validation have null values when a validator is called. This means a single validator can be called several times for a single file, each time testing if the required field is not null.

function maxDimensions(width, height) {
  return function(fileInfo) {
    var imageInfo = fileInfo.originalImageInfo;
    if (imageInfo !== null) {
      if (imageInfo.width > width || imageInfo.height > height) {
        throw new Error('dimensions');
      }
    }
  };
}

In-widget validation

Validators can also be added to the widget through its validators property. This works for both single-file and multi-file widgets.

var widget = uploadcare.Widget('#uploadcare-file');
widget.validators.push(imagesOnly);

widget.validators is a regular javascript array. One widget can be assigned several validators, and one validator can be assigned several widgets.

firstWidget.validators.push(imagesOnly);
secondWidget.validators.push(imagesOnly);
firstWidget.validators.push(maxSize(1024 * 1024));
firstWidget.validators.push(maxDimensions(800, 600));
firstWidget.validators; // [function imagesOnly(){...}, function () {...}, function () {...}]

Note, you can’t rewrite validators with another object, you need to remove them from an original array.

// Leave only first two validators:
widget.validators = widget.validators.splice(0, 2); // WRONG!
widget.validators.splice(2); // Correct.

// Remove all validators:
widget.validators = []; // WRONG!
widget.validators.length = 0; // Correct.

Validation in JS API

You can add a validators array to a Settings object of any API function where it makes sense,

uploadcare.openDialog(null, {
  validators: [
    imagesOnly,
    maxSize(1024 * 1024),
    minDimensions(800, 600)
  ]
});

The list of such functions includes but is not limited to: openDialog, openPanel, fileFrom, filesFrom, dragdrop.uploadDrop, dragdrop.receiveDrop.

It is better to use validators instead of checking file properties in widget.onChange and widget.onUploadComplete callbacks. Validators are activated before a dialog gets closed thus improving your UX.

Error messages

Each argument in the Error constructor is a key in your locale file. Such keys are mapped to messages shown to your users. The keys and messages can either be predefined like size, image, or custom.

We’re always happy to help with code, integration, and other stuff. Search our site for more info or post your question in our Community Area.