简体   繁体   English

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

[英]knockout.js validation error count not setting

I'm using the knockout.js validation plugin and it's working perfectly for a validating a form that I'm using, however, it doesn't appear to be tracking the number of errors. 我正在使用基因敲除插件(knockout.js)验证插件 ,它非常适合验证我正在使用的表单,但是,它似乎并未跟踪错误的数量。 When I submit the form, if it detects any errors it shouldn't submit, but it is submitting. 当我提交表单时,如果它检测到任何错误,则不应提交,而是正在提交。

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

I could be approaching this entirely wrong, I'm just basing it off of this fiddle that is linked from the Github page for the project 我可能会遇到完全错误的情况,我只是基于这个项目的Github页面链接的小提琴

have you tried changing submitOrder to reference self instead of the function name 您是否尝试过更改SubmitOrder来引用self而不是函数名

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

and inside the function, make the group call 然后在函数中进行群组通话

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

assigning to self.errors is actually redundant. 分配给self.errors实际上是多余的。 the group call will create an errors observable on self 小组通话将产生一个可观察到的错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM