Search code examples
asp.net-mvc-4knockout.jsknockout-validation

Knockout validation group undefined or unable to location errors in MVC


I'm trying to work on knockout and unable to validate using knockout validation plugin. [EDIT: update fiddle] sample fiddle: jsfiddle.net/EHDD8

           var CustVM = function () {
        var self = this;
        self.name = ko.observable().extend({ required: "Name is required" });
        self.contact = ko.observable();
        self.phone1 = ko.observable();
        self.email = ko.observable().extend({email: true});
        self.website = ko.observable().extend({required: "Website is required"});

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

         self.save = function () {
            if (self.isValid()) {
                alert("no error");
            }
            else {
                alert("error");
            }
            alert("save clicked ");
        };

        self.cancel = function() {
            alert("cancel clicked");
        };
    };

    ko.applyBindings(new CustVM());

isValid is true even though i have not entered any required elements.


Solution

  • You are not using

    ko.validation.registerExtenders();
    

    that's why you are having problem, you can check my this ARticle:-

    http://www.c-sharpcorner.com/UploadFile/cd7c2e/apply-knockout-validations-in-mvc-application/