Add new Form validation handler

Alfresco share user interface is form based. Where each form consist of multiple fields and for all those different type of fields template is defined. Along with templates there are validation handlers and renderers are defined in javascript functions. Now if there is a situation where none of the out of box validator is matching our requirement we might need to define our custom validator. Validation handler is a small JavaScript function that gets called by the forms runtime when a field value needs to be validated.The interface for a validation handler is shown below.

 * Validation handler for a field.
 * @param field {object} The element representing the field the validation is for
 * @param args {object} Object containing arguments for the handler
 * @param event {object} The event that caused this handler to be called, maybe null
 * @param form {object} The forms runtime class instance the field is being managed by
 * @param silent {boolean} Determines whether the user should be informed upon failure
 * @param message {string} Message to display when validation fails, maybe null
 * @static
function handler-name(field, args, event, form, silent, message)

Now if you want to have your own handler you need to implement this interface.

field: HTML DOM element representing the field’s value, which is normally an HTML input DOM element, so that the value property can be accessed.

args: It dependent on the handler being implemented. By default, these will be the parameters of the constraint defined on the field.

You can see rest of the details related to parameters from interface documentation on above code snippet.

Validation handler is responsible for taking the value from the field and uses the argument(args) parameter to calculate whether the current value is valid or not.

Here is how your custom validation handler looks like.

var _validFileExtensions = [".jpg", ".jpeg", ".bmp",".png"];
Alfresco.ValidateImageExtension = function ValidateImageExtension(field, args, event, form, silent, message) {
    if (field.type == "file") {
        var sFileName = field.value;
        if (sFileName.length > 0) {
            var isValid = false;
            for (var j = 0; j < _validFileExtensions.length; j++) {
                var sCurExtension = _validFileExtensions[j];
                if (sFileName.substr(sFileName.length - sCurExtension.length, sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) {
                    isValid = true;
            if (!isValid) {
                return false;
    return true;

This validation handler is making sure about type of images which is being uploaded. It restrict image extension of given list of extension. If any other type of image comes in then it will generate validation error.

You also need to register javascript file in which you have placed handler function. For instance file is customValidator.js then entry in share-config-custom.xml will be required.

                <js src="/extras/components/form/customValidator.js"/>              

This will make sure javascript file is loaded in classpath.This handler can be used as follow in your custom javascript file.

 var imagesInput = Dom.getElementsByClassName("images-upload");
customForm.addValidation(, Alfresco.ValidateOmageExtension, null, "change", "Wrong File Extension");

In above two lines we are associating our custom handler with one of the field from form which is responsible for adding images.In case of error it will show error message which is one of argument of fucntion addValidation().

Summary: We have gone through what are validation handlers  in alfresco share form and how can we add new validation handler in alfresco share.

Looking for quality Alfresco Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , | Posted under Alfresco | RSS 2.0

Author Spotlight


I love opensource technologies working with those technologies from the time I have stepped in to the Software Industry. Alfresco CMS is my area of expertise. I have worked on various complex implementations which involved integration of Alfresco with other technologies, extensively worked with JBPM workflows and Webscripts.

Leave a Reply

Your email address will not be published. Required fields are marked *