I am trying to validate my model manually by calling self.isModelvalid
in my test and assuming that I would return true
, because before calling it I assigned test values to all properties in my model . here is how I call in my test
listingViewModel = new ListingViewModel();
listingViewModel.country('a');
listingViewModel.city('a');
listingViewModel.cummunity('a');
listingViewModel.subCummunity('a');
listingViewModel.postCode('1');
expect(listingViewModel.isModelValid()).toBe(true);
and this is my source class where my model and all methods exists :
function validateModel(model) {
var self = model;
for (var prop in self) {
if (self.hasOwnProperty(prop)) {
if (!(self[prop].isValid())) {
return false;
}
}
}
return true;
}
function ListingViewModel() {
var self = this;
self.country = ko.observable('').extend({required : true});
self.city = ko.observable('').extend({ required: true });
self.cummunity = ko.observable('').extend({ required: true });
self.subCummunity = ko.observable('').extend({ required: true });
self.postCode = ko.observable('').extend({ required: true });
self.isModelValid = function () {
validateModel(self);
};
self.addListing = function () {
};
}
ko.validation.configure({
decorateElement: false,
errorElementClass: "error", // class name
insertMessages: false,
grouping: { deep: true, observable: true }
});
When when i run it says
listingViewModel.isModelValid() TypeError: self[prop].isValid is not a function
and i think this error occurs when it validateModel
try to validate isModelValid
and it fails because isModelValid
is not a function its property .
So how to validate only properties in a knockout model .
Thanks