2013-03-18 89 views
0

我有定义的视图模式下获得可观察到的字段的验证结果之一,在计算字段:如何利用基因敲除验证框架

var ViewModel = function() { 
    var self = this; 
    self.name = ko.observable().extend({ required: true }); 
    self.identityCode = ko.observable().extend({ required: true, maxLength: 18, minLength: 15 }); 
    self.gender = ko.computed(function() { 
     // get gender information from the identiy code here 
    }); 
    self.birthdate = ko.computed(function() { 
     // get birthdate information from the identity code here 
    }); 
    self.form_onsubmit = function (form) { 
     if (!self.isValid()) { 
      self.errors.showAllMessages(); 
      return false; 
     } else { 
      return true; 
     } 
    }; 
}; 

,你可以看到上面的代码中,性别字段和brithdate场均计算的字段从身份代码中获得。我只想知道如何在完成之前获得身份代码的验证结果。谢谢!

回答

0

的验证可观察被扩展与计算isValid。因此,您可以检查与结果:

self.gender = ko.computed(function() { 
     // get gender information from the identiy code here 
     if(self.identityCode.isValid()) { 
      // do something with the code 
     } 
    });