Question

Field Validation

Hi everyone,

We are trying to add a validation to ensure that the field "CPF" on Contact is not duplicated.

After debugging, we could see that the code is correct, but the validation message is returned before the end of the query execution.

Is there a way to garantee that the validation message is only returned after the end of the query?
 

//Redefining the base function initiating custom validators
setValidationConfig: function () {
    //Calls the initialization of validators for the parent view model
    this.callParent(arguments);
    //Adds the cpfValidator method for the [UsrCPF] column
    this.addColumnValidator("UsrCPF", this.cpfValidator);
},
//Checks if the CPF is valid
cpfValidator: function () {
    //Variable for storing a validation error message.
    invalidMessage = "";
   
    //Gets the Contact's CPF
    var cpf = this.get("UsrCPF");
   
    if (cpf != null && cpf != "")
    {
        //Creates a query for Contact
        var select = Ext.create("Terrasoft.EntitySchemaQuery", {
            rootSchemaName: "Contact"
        });
       
        //First filter (CPF)
        var selectFirstFilter = select.createColumnFilterWithParameter(Terrasoft.ComparisonType.EQUAL, "UsrCPF", cpf);
       
        //Second filter (ID)
        var selectSecondFilter = select.createColumnFilterWithParameter(Terrasoft.ComparisonType.NOT_EQUAL, "Id", this.get("Id"));
       
        //Logical operator to be used between the filters
        select.filters.logicalOperation = Terrasoft.LogicalOperatorType.AND;
       
        //Adds the created filters to the collection
        select.filters.add("selectFirstFilter", selectFirstFilter);
        select.filters.add("selectSecondFilter", selectSecondFilter);
       
        //Executes the query
        select.execute(function (response) {
            if (response.success) {
                //Checks if there's more than one Contact with the given CPF
                if (response.collection.getCount() > 0) {
                    //Console message for log
                    console.log(" CPF: " + cpf +
                                " Total: " + response.collection.getCount());
                    invalidMessage = this.get("Resources.Strings.CpfAlreadyExists");
                }
            }
        }, this);
    }
   
    //If the validation is successful, empty strings are returned to the object
    return {
        //Validation error message displayed in the data window when saving a page
        fullInvalidMessage: invalidMessage,
        //Validation error message displayed under the control item
        invalidMessage: invalidMessage
    };
}

 

File attachments
Like 1

Like

3 comments

select.execute is asynchronous, so you function will not wait for the respoonse.

What I mean is that the validator will execute till the end without waiting for the execute to finish ..

Unless you can make the select.execute to run synchronously you cant solve the problem .. sorry

Hello All,



How to make the method run synchronously?

Similarly, to that we have an esq method "esq.getEntityCollection" which runs asynchronously, which leads to the same issue as mentioned in the above post. 



Is there a way to use esq for the addcolumnValidator() method and achieve the result?

 

Adharsh,

You need to use asyncValidate to perform an asynchronous query in the validation. See 

https://customerfx.com/article/asynchonous-validation-on-pages-in-creat…

Ryan

Show all comments