function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
indyindy 

not able to perform custom validation on Lightning component due to asynch nature.

I am not able to show the custom validation error message as a result of  callback method.

Controller.js
saveCase : function(component, event, helper){
       //existing validations...
       var validTab1 = helper.validateTab1(component); 
       var validTab2 = helper.validateTab2(component); 
       
        //New validaiotn      
        var isValidDate =           helper.validateExtDate(component);
        // Other business validations....
        helper.createCase(compoent);

}


Helper.js
 
validateExtDate:function(component)
{
    var caseRec = component.get("v.case");
    var action = component.get("c.getResponseDeadlineOnEdit");
    action.setParams({
                                "cs":caseRec
    });

    action.setCallback(this,function(response){
             this.processresults(response, component);
    });
    $A.enqueueAction(action);
},

processresults : function(response,component) {
    var extnsDate = component.find("respExtdate");
    var extnsDateVal = extnsDate.get("v.value");
    var extDate = new Date(extnsDateVal);

    var state = response.getState();
    if(state === "SUCCESS") {
         var deadlinedate= response.getReturnValue();      
         if(extDate <=  deadlineDate) {
                    extnsDate.set("v.errors", [{message: "Please select a different date” }]);
        }
}

The contoller.js is executing all the helper methods including the new asynch/callback method in helper.  But it is not stopping the flow if the custom validation occurs. i.e Due to asynch behaviour the flow is executing the existing createCase method, which save the case even if there is a validation. 
Ravi Dutt SharmaRavi Dutt Sharma
Hi Indy,

You should call createCase in the callback of validateExtDate function. Below code should help:
saveCase : function(component, event, helper){
       //existing validations...
       var validTab1 = helper.validateTab1(component); 
       var validTab2 = helper.validateTab2(component); 
       
        //New validaiotn      
        var isValidDate =           helper.validateExtDate(component);
        // Other business validations....
        //helper.createCase(component); - Do not call this method here

}
 
validateExtDate:function(component)
{
    var caseRec = component.get("v.case");
    var action = component.get("c.getResponseDeadlineOnEdit");
    action.setParams({
                                "cs":caseRec
    });

    action.setCallback(this,function(response){
             this.processresults(response, component);
    });
    $A.enqueueAction(action);
},

processresults : function(response, component) {
    var extnsDate = component.find("respExtdate");
    var extnsDateVal = extnsDate.get("v.value");
    var extDate = new Date(extnsDateVal);

    var state = response.getState();
    if(state === "SUCCESS") {
        var deadlinedate= response.getReturnValue();      
        if(extDate <=  deadlineDate) {
            extnsDate.set("v.errors", [{message: "Please select a different date” }]);
        } else {
            this.createCase(component);       
        }
}


 
indyindy
Hi Ravi,

Thank You for your reply. I have a question here, we are comparing the dates based on the results from the callback. How Salesforce(Lightning) is getting the results promptly here.

i.e  The dealineDate in line number 22 in the above code is a result of Callback.  To my understanding the moment when a callback is invoked it will be placed in a separate thread and the rest of the statements will execute sequentially.  So how the condition extDate <= deadlineDate(line 23) will be evaluated.  will dealineDate variable holds the value from Callback function. ?

Please clarify.

Thanks,
Indy
Ravi Dutt SharmaRavi Dutt Sharma
Refer to my code for line numbers. At line 12 in validateExtDate method, we are enqueuing the action, ie, the action will be placed in a queue for the server side call to happen. Once the server side call is completed, the execution comes at line 10. So its not a line by line execution here. First the action is queued, then the action executes and completes. Once the action is completed, only then in come in the callback method and executes the processresults methods.
indyindy
Thank You for your response and sorry for the delayed reply.

Ok. So you are telling once the lifecycle is   Call is queued --> Completed -- > Calls SetCallback.

My question here is ,  in my original code when the validateExt() method is invoked from Controller JS(line 7), The framwork has to complete the validateExt() method lifecycle and show the error message/stop the flow of execution. But it is moving forward with CreateCase() method.