knockout.js验证错误计数未设置

时间:2013-05-25 21:12:45

标签: javascript knockout.js

我正在使用knockout.js validation plugin,它正在完美地用于验证我正在使用的表单,但是,它似乎没有跟踪错误的数量。当我提交表单时,如果它检测到任何错误,则不应提交,但它正在提交。

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

1 个答案:

答案 0 :(得分:2)

您是否尝试将submitOrder更改为引用self而不是函数名称

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实际上是多余的。组调用将在自我

上创建一个可观察到的错误