Knockoutjs验证问题 - self.errors()。长度

时间:2014-05-13 10:04:44

标签: jquery asp.net-mvc validation asp.net-mvc-4 knockout.js

我已经包括:

knockout.3.0.1.js

knockout.validation.debug.js

jquery.validate.js

错误是:(来自Firebug)

TypeError: self.errors is not a function

除了验证之外,所有其他代码都是完美的。

我的代码

var UserViewModel = function (UserName, Password) {
    var self = this;

    self.UserName = ko.observable(UserName).extend({
        required: {
            message:"Please enter User Name"
        }
    });
    self.Password = ko.observable(Password).extend({
        required: {
            message: "Please enter Password"
        }
    });


    this.loginClick = function () {

        if (self.errors().length == 0)
        {
            alert('no errors');
          //  var strJSON = ko.toJSON(self); //Convert whole model to json
        }
        else
        {
            alert("Please check your submission");
            self.errors.showAllMessages();
        }
        alert("function called");

    };
};

ko.applyBindings(new UserViewModel('', ''));

1 个答案:

答案 0 :(得分:4)

您忘了创建验证组:

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

创建验证组,错误将被解决