我有以下定义的视图模型:
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 :(得分:0)
经过验证的observable扩展为计算isValid
。因此,您可以使用以下方法检查结果:
self.gender = ko.computed(function() {
// get gender information from the identiy code here
if(self.identityCode.isValid()) {
// do something with the code
}
});