Knockout ViewModel isValid error when using the knockout verification plugin

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 () { //TODO: according to the documentation you should be able to just //check self.isValid() but that throws an error that there is no //such method on the object? dunno why. if (!self.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')); 
+4
source share
2 answers

Call ko.validation.group in your virtual machine to group all the monitored observable data at the VM level. Then isValid will be true only if the child has no observable errors.

Some other SO answers about ko.validation.group

How to use the ko.validation.group function

Knockout check ko.validation.group vs ko.validatedObservable

+4
source

Just thought that I would post the actual code that I need to use. Thanks to ragnarok56 for pointing me in the right direction. I obviously spent too little time on the documentation.

I just added this line of code above the call to check isValid () in the view model

 var result = ko.validation.group(viewModelInstance, { deep: true }); 
+5
source

Source: https://habr.com/ru/post/1483238/


All Articles