Search code examples
knockout-validation

How to call isValid() function in the viewmodel using knockout-validation


I have a viewmodel defined following:

var ViewModel = function() {
    var self = this;
    self.property1 = ko.observable().extend({ required: true });
    self.property2 = ko.computed(function() {
        return self.property1();
    });
    self.form_onsubmit = function (form) {
        if (!self.isValid()) {
            console.log("error");
        }
        return false;
    };
};

$(function () {
    ko.applyBindingsWithValidation(new ViewModel());
});

when i call the form_onsubmit function, an error occured:

TypeError: self.isValid is not a function
if (!self.isValid()) {

how to solve it, thanks^^^


Solution

  • add

    self.errors = ko.validation.group(self);

    to your viewmodel