Question

Perform Asynchronous Validation

I achieved Asynchronous Validation when I have One.  But How to implement asynchronous validation when I have multiple asynchronous validations to perform

Like 1

Like

5 comments
Best reply

Kavya,

I simplified the code from somewhere else and omitted an important part (the part that checks the previous validation functions result). I've updated the code in my original reply. Change yours like that and it should work. 

I've also written up the steps here as well: https://customerfx.com/article/validating-multiple-asynchronous-results…

Ryan

Hello,

 

Could you please describe your issue in more details?

Set up each validation as it's own function, something like this for each validation function: 

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, you'll use Terrasoft.chain to chain all the validations together, something like this (in this example I have two validation functions "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]);
}

Ryan

Ryan Farley,

Hey Ryan , Thanks for the response,

I tried the same something like below but not able to achieve the target. Error Message is not coming and all records are saving which I ideally want to restrict from save as it is invalid,











 

asyncValidate: function(callback, scope) {

                this.callParent([function(response) {

                    if (!this.validateResponse(response)) {

                        return;

                    }

                    Terrasoft.chain(

                        this.startDateValueValidator,

                        this.CheckPreviousAppointment,

                        function(next) {

                            callback.call(scope, response);

                            next();

                    }, this);

                }, this]);

            },

            startDateValueValidator: function(callback, scope) { 

               var StartDate = this.get("StartDate");

              // this.set("StartDateChangeFlag",false);

               if (!StartDate) {

                        callback.call(scope, {

                            success: true

                        });

                    return;

                }

                if (!this.get("StartDateChangeFlag")) {

                    console.log("Skipppedddd Start Date Validator");

                    callback.call(scope, {

                            success: true

                        });

                   return;

                }

               console.log("Inside Start Date Validator");

               var today  = new Date();

              // today.setHours(0, 0, 0, 0);

               if(StartDate < today){

                    callback.call(scope || this, {

                                        success: false,

                                        message: "Start Date can't be Past."});

                 }    else{

                  console.log("Inside else part");

                   callback.call(scope || this, {

                            success: true

                        });

              }

            },

            CheckPreviousAppointment: function(callback, scope) {

               var MemberMapped = this.get("UsrUsrMember");

               var AppointmentStatus = this.get("Status");

               var startDate = this.get("StartDate");

                if (!MemberMapped) {

                    callback.call(scope || this, {

                            success: true

                        });

                    return;

                }

                console.log("Inside Member Validator");

                // create query for server side

                var esq = Ext.create("Terrasoft.EntitySchemaQuery", { rootSchemaName: "Activity" });

                esq.addColumn("Id"); 

                esq.filters.addItem(

                    esq.createColumnFilterWithParameter(Terrasoft.ComparisonType.EQUAL, "UsrUsrMember", MemberMapped)

                );

                esq.filters.addItem(

                    esq.createColumnFilterWithParameter(Terrasoft.ComparisonType.EQUAL, "Status", AppointmentStatus)

                );

                esq.filters.addItem(

                    esq.createColumnFilterWithParameter(Terrasoft.ComparisonType.EQUAL, "StartDate", startDate)

                );

                esq.getEntityCollection(function(response) {

                    if (response.success && response.collection) {

                        var items = response.collection.getItems();

                        if (items.length > 0) {

                            console.log("Length : --> "+items.length);

                            console.log("Inside Callback if");

                                callback.call(scope || this, {

                                    success: false,

                                    message: "Appointment is already booked. Kindly Cancel the previous to book new appointment"        

                                });

                        } else{

                            callback.call(scope || this, {

                                success: true

                            });

                        }

                    }

                }, this);

            },

Kavya,

I simplified the code from somewhere else and omitted an important part (the part that checks the previous validation functions result). I've updated the code in my original reply. Change yours like that and it should work. 

I've also written up the steps here as well: https://customerfx.com/article/validating-multiple-asynchronous-results…

Ryan

Is there any published methodology for doing this for async validation in Freedom UI?

Show all comments