Client side validation before save


Sometime you might want to do some custom validation on web app or just to prompt user message with confirmation. This might be achieved by "injecting" your own update handler. Snippet below explains how it could be done.

    
    /* Saving ref to default handler */
    var vDefaultUpdateHandler = dsDataObject.getDataHandler().update;
    /* Changing handler to custom */
    dsDataObject.getDataHandler().update = function (pData, pCallback) {
        /* Calling some validation, prompt... */
        asyncValidation(function (pIsValid) {
            if (pIsValid) {
                /* If valid then continue with default handler just bypassing parameters */
                vDefaultUpdateHandler.call(dsDataObject.getDataHandler(), pData, pCallback);
            } else {
                /* setting proper currentIndex, might be different if save was triggered by index change */
                dsDataObject.setIndexFromDictionary({ PrimKey: pData.PrimKey }); 

                dsDataObject.cancelEdit();

                /* Map correct fields */
                var vCurrentRow = dsDataObject.getFields().map(function (pField) {
                    /* We need to return only fields which are in select list, not dynamic fields as af_Selected  */
                    if(!pField.af_Selected)
                        return dsDataObject.currentRow()[pField.name];
                })

                pCallback(null, vCurrentRow); 
            }
        });
    }

    function asyncValidation(pCallback) {
        setTimeout(function () { // simulate async statement
            if (confirm("Proceed?")) {
                pCallback(true);
            } else {
                pCallback(false);
            }
        }, 1000);
    }