Search code examples
jqueryknockout.jsknockout-validation

Knockout Validation Custom rules are not working after upgrading jquery version from 2.1 to 3


I am using knockout 3.2 . I have a field on which a custom knockout rule is added which get its value from an ajax call. It was working fine with Jquery version 2.1. But when I upgraded the jquery version to 3.0, it somehow stopped working. Below is the sample code.

Validation on field

self.ViewModel.Id.extend(
{
idExist:{param:true}
});

Custom Rule (The ajax call is done with param async:false)

ko.validation.rules.idExist = {
 validator:function(id, validate){
 var idExist = false;
 $.when(
    $.ajax())
      .then(function(data){
           idExist  = data;
    });
 return idExist ;
 },
 message:"Id not exist"
 }

After some analysis, I found that after upgradation of jquery, the validator is not waiting for the ajax call to over and directly returning idExist =false.

Any ideas? Thanks.


Solution

  • The issue got resolved by adding async:true.PFB the updated code.

     validator:function(id, validate){
     var idExist = false;
     $.when(
        $.ajax())
          .then(function(data){
               idExist  = data;
        });
     return;
     },
     message:"Id not exist",
    async:true
     }