Returning invalidMessage Before Validation Completes in Custom Duration Validator
Hello everyone,
I'm working on a custom duration validator in Creatio, but I'm running into an issue where the function returns invalidMessage
before the validation logic completes.
Here’s a simplified version of my function:
durationValidator: function(){ var invalidMessage=""; var name=this.get("UsrNameLookup").value ?? null; var currentDuration=this.get("UsrDurationMinute"); if(!name){ return; } var esqDuration = this.Ext.create("Terrasoft.EntitySchemaQuery", { rootSchemaName: "UsrPerformances" }); esqDuration.addColumn("UsrDurationMinute"); // var groupFilters = this.Ext.create("Terrasoft.FilterGroup"); var totalDuration = 0; if(this.isAddMode()){ var nameFilter = this.Terrasoft.createColumnFilterWithParameter(this.Terrasoft.ComparisonType.EQUAL, "UsrNameLookup", name); // groupFilters.addItem(filterId); esqDuration.filters.add("esqFirstFilter",nameFilter); esqDuration.getEntityCollection(function(result) { if (!result.success) { this.showInformationDialog("Request error"); return; } else { result.collection.each(function(item) { totalDuration += item.get("UsrDurationMinute"); } ); } totalDuration += currentDuration; console.log(totalDuration); this.Terrasoft.SysSettings.querySysSettingsItem("UsrMaximumDuration", function(maxDuration) { // console.log("td"+totalDuration); if (totalDuration > maxDuration) { this.set("Numbervalue", false); this.showInformationDialog("No more than " + maxDuration + " total performances duration is allowed."); invalidMessage= "No more than " + maxDuration + " total performances duration is allowed."; return{ invalidMessage: invalidMessage }; } else { this.set("Numbervalue", true); invalidMessage=""; return{ invalidMessage: invalidMessage }; } }, this); }, this); }else{ console.log("else"); var nameFilter = this.Terrasoft.createColumnFilterWithParameter(this.Terrasoft.ComparisonType.EQUAL, "UsrNameLookup", name); var idFilter = this.Terrasoft.createColumnFilterWithParameter(this.Terrasoft.ComparisonType.NOT_EQUAL, "Id", this.get("Id")); esqDuration.filters.logicalOperation = Terrasoft.LogicalOperatorType.AND; esqDuration.filters.add("esqFirstFilter",nameFilter); esqDuration.filters.add("esqSecondFilter", idFilter); esqDuration.getEntityCollection(function(result) { if (!result.success) { this.showInformationDialog("Request error"); return; } else { result.collection.each(function(item) { totalDuration += item.get("UsrDurationMinute"); } ); // console.log("i am running"); // let prevduration=this.get("UsrprevDuration"); // console.log("previous duration"+ prevduration); totalDuration += currentDuration; } console.log(totalDuration); this.Terrasoft.SysSettings.querySysSettingsItem("UsrMaximumDuration", function(maxDuration) { // console.log("td"+totalDuration); if (totalDuration > maxDuration) { this.set("Numbervalue", false); this.showInformationDialog("No more than " + maxDuration + " total performances duration is allowed minutes."); invalidMessage="No more than " + maxDuration + " total performances duration is allowed."; return{ invalidMessage: invalidMessage }; } else { this.set("Numbervalue", true); invalidMessage=""; return{ invalidMessage: invalidMessage }; } }, this); }, this); } console.log(invalidMessage); return{ invalidMessage: invalidMessage }; }, The console.log(invalidMessage); statement always prints an empty string.
- The function returns
<strong>invalidMessage</strong>
before the asynchronous operations (getEntityCollection
andquerySysSettingsItem
) complete. - As a result, validation messages do not appear correctly.
Like
Use asynchonous validation instead, see https://customerfx.com/article/asynchonous-validation-on-pages-in-creat…
Jerome BERGES,
Thanks for answering, I tried different approach like this https://academy.creatio.com/docs/8.x/dev/development-on-creatio-platform/getting-started/first-app/develop-application/step-3-add-page-validation and it works.
Is there any i can perform same validation on detail of some custom page.
Darshan Dev Prajapat,
The problem with the article you linked to and what you're trying to accomplish is that you're using asynchronous methods inside the validator, which won't work with a synchronous validation method since the validation method will return before you've retrieved the asynchronous results. You need to switch to the asyncValidate (see link provided by Jerome) in order to use asynchronous methods (such as EntitySchemaQuery, SysSettings retrieval, etc).
The main difference in the article you linked to and your code is that the article performs the ESQ outside of the validator, when the onEntityInitialized fires, not inside the validator (which is what you're doing). To do asynchronous operations like ESQ (and using SysSettings) you need to use the asyncValidate method.
Ryan
Ryan Farley,
Thanks for the detailed explanation. I am using onEntityInitialized
for validation, and it is working for me. However, I would like to know if there is a way to perform the same validation where this performance detail is being used, such as on a custom page.