2013-05-25 87 views
1

我使用的是knockout.js validation plugin,它對我正在使用的表單進行驗證非常有效,但是它似乎沒有跟蹤錯誤的數量。當我提交表格時,如果它檢測到任何錯誤,它不應該提交,但它正在提交。knockout.js驗證錯誤計數未設置

function EntryViewModel(fullName, addressLine1, addressLine2, city, state, ZIP, email) { 

    //viewmodel code here 
    //the validation messages are being set on my form so i figure listing all of the rules is not necessary 

    self.submitOrder = function(){ 
    alert(EntryViewModel.errors().length); //displays 0 
    if (EntryViewModel.errors().length == 0) { 
      //submit 
    } 
    else{ 
     alert('Please fix errors before submitting'); 
    } 
    } 
} 

EntryViewModel.errors = ko.validation.group(EntryViewModel); //I also tried putting this in my viewmodel and it didn't do anything 

我可以處理這個完全錯誤的,我只是立足它關閉的是從GitHub的友情鏈接的項目this fiddle

回答

2

你試圖改變submitOrder引用自我,而不是功能命名

self.submitOrder = function(){ 
    alert(self.errors().length); //displays 0 
    if (self.errors().length == 0) { 
     //submit 
    } 
    else{ 
     alert('Please fix errors before submitting'); 
    } 
} 

,並在函數內部,進行羣組通話的

self.errors = ko.validation.group(self); 
分配給self.errors的

實際上是多餘的。羣組電話會產生一個可觀察到的錯誤

+0

就是這樣,謝謝! – user1443519