Search code examples
javascriptjqueryknockout.jsknockout-validation

validateObservable in Knockout JS - undefined is not a function


Good day,

I am trying to validate a pretty huge form on my side, and basically rewritten all of my viewModel code so it can be validated. Problem is that ko.validatedObservable() doesn't seem to exist for some reason. I have used the code from this site, and I honestly cannot see what's the problem in my code. Here is a sample:

var someNameSpace= {};

    someNameSpace.bindData = function () {
        someNameSpace.viewModel =
        someNameSpace.initViewModel("123456", "username1", "address1", "address2", "city");

        ko.applyBindings(this.viewModel);
    }

someNameSpace.initViewModel = function (partnerId, username, address1, address2, city) {
    console.log(ko);
    var someViewModel = ko.validatedObservable({ //<----FAILS HERE
        partnerId: ko.observable(partnerId).extend({ required: "This field is required", number: true }),
        username: ko.observable(username).extend({ required: "This field is required" }),
        address1: ko.observable(address1).extend({ required: "This field is required" }),
        address2: ko.observable(address2).extend({ required: "This field is required" }),
        city: ko.observable(city).extend({ required: "This field is required" }),
    });

    var validationOptions = { insertMessages: true, decorateElement: true };
    ko.validation.init(validationOptions);

    return someViewModel ;
}

$(document).ready(function () {
    someNamespace.bindData();
});

On that site, it claims to use the ko.validatedObservable in order to inspect whether or not fields were correctly entered by the user. I am still very new to knockoutJS - what can I do?

error


Solution

  • You need to include a reference to the 'knockout.validation.js' library.

    knockout.validation.js

    https://rawgit.com/ericmbarnard/Knockout-Validation/master/Src/knockout.validation.js

    GitHub reference

    https://github.com/Knockout-Contrib/Knockout-Validation