I am new to using knockout and I am trying to use a knockout verification plugin along with a display plugin. I have a problem with the isValid function on a view model object. According to the documentation, isValid should return a bool to determine if any of the views that are visible in the model are valid, but when I call it, I get an error that isValid is not a function. However, if I name isValid on the observables themselves, it works fine. The problem is that I use the mapping plugin with some dynamic data objects that I get from the server, so I donβt necessarily know the names of the observables that I need to check, so it is not practical to check them separately. The example below is simplified, but in the actual implementation I do not know the names of the observables. Maybe I just skipped the documentation?
Thank you for your time.
It works
var dataItem = { FirstName: '', LastName: '', Age: '', Email: '' } var viewModel = function(data) { var self = this; this.Email = ko.observable().extend({ email: true }); this.LastName = ko.observable().extend({ required: true }); this.Age = ko.observable().extend({ required: true, min: 0 }); this.saveClick = function () { if (!self.Email.isValid() || !self.Age.isValid() || !self.LastName.isValid()) { alert('Not valid'); else { alert('Valid'); } }; this.cancelClick = function () { ko.mapping.fromJS(dataItem, null, this); } ko.mapping.fromJS(data, {}, this); }; var viewModelInstance = new viewModel(dataItem); ko.applyBindings(viewModelInstance, document.getElementById('bindingDiv'));
But it does not work
var dataItem = { FirstName: '', LastName: '', Age: '', Email: '' } var viewModel = function(data) { var self = this; this.Email = ko.observable().extend({ email: true }); this.LastName = ko.observable().extend({ required: true }); this.Age = ko.observable().extend({ required: true, min: 0 }); this.saveClick = function () {
source share