You need to sign in to do that
Don't have an account?
indy
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
Helper.js
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.
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.
You should call createCase in the callback of validateExtDate function. Below code should help:
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
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.