2014-05-13 63 views
0

我已經包括:Knockoutjs驗證問題 - self.errors()長度

knockout.3.0.1.js

knockout.validation.debug.js

jquery.validate.js

錯誤是:(來自螢火蟲)

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('', '')); 
+0

你能提供的jsfiddle,很明顯,錯誤屬性不在自我範圍內呈現 –

+0

http://jsfiddle.net/d8heg/ - fsfiddle –

+0

你在哪裏def ine self.errors()? –

回答

4

你忘了創建驗證組:

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

創建驗證組,錯誤將得到解決