简体   繁体   中英

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. 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

have you tried changing submitOrder to reference self instead of the function name

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. the group call will create an errors observable on self

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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