
In a previous article I showed how to asynchronously validate a result when saving a page in Creatio. This is needed when validating something when a page is saved and the validation requires an asynchronous function, such as an EntitySchemaQuery. At the end of that article, I mentioned how you would go about doing this for validating multiple asynchronous results at the same time by using Terrasoft.chain to chain all the asynchronous validation function together. In this article I will describe how to do this in detail.
Refer to the original article forĀ Asynchronous Validation on Pages in Creatio
In order to do this, it is easiest if you split up each asynchronous validation into it’s own function. This function would look like this:
validateThing1: function(callback, scope) { var result = { success: true, message: "" }; // do whatever async call to validate and then call below to return the result, // setting result.success = true if validation passed or result.success = false // if failed. If the validation failed, also set the message (will be displayed to user) callback.call(scope || this, result); }
Then, in the asyncValidate function itself, you’ll use Terrasoft.chain to chain all the validation function together. In this scenario, I have two validation functions, structured like the sample above, that are named validateThing1 and validateThing2.
asyncValidate: function(callback, scope) { this.callParent([function(response) { if (!this.validateResponse(response)) { return; } Terrasoft.chain( function(next) { this.validateThing1(function(response) { if (this.validateResponse(response)) { next(); } }, this); }, function(next) { this.validateThing2(function(response) { if (this.validateResponse(response)) { next(); } }, this); }, function(next) { callback.call(scope, response); next(); }, this); }, this]); }
Now, when the page is saved, each validation function will be chained, each executing the next, before the final result is obtained to determine if the validation was successful or not.
This is great. Thank you.