Sections - matthewnitschke/Kavie GitHub Wiki

Sections in kavie are ways to validate different parts of a view model.

self.value = ko.observable().extend({
  kavie:{
    required: true,
    section: "basicInfo" // assign a kavieObservable to a section
  }
});

self.valueTwo = ko.observable().extend({
    kavie: {
      required: true,
      section: "basicInfo" // assign a kavieObservable to the same section
    }
});

// some other observable in the same viewmodel
self.otherValue = ko.observable().extend({
  kavie: {
    required: true
  }
})

// validates all observables in the basicInfo section
// it will not validate the "otherValue" observable as it is not in the basicInfo section
Kavie.isValid("basicInfo"); 

Section Validators

You can add validators to sections. These validators get applied to every observable in the section, including children sections

self.value = ko.observalbe().extend({
  kavie: {
    section: "mySection",
    minLength: 3
  }
});

// makes every observable in mySection have a required validator on it
Kavie.addSectionValidators("mySection", {
  required: true
});

Kavie.isValid("mySection");

Variable Validation

Sections have an ability to dynamiclly turn on and off their validation. This can be helpful when validating against optional areas of a form but when enabled still want the entire form to be filled out. In this example the kavie section will only be validated if the boolean observable validate is true

self.value = ko.observable().extend({
  kavie:{
    section: "dynamicSection"
  }
});

self.validate = ko.observable(true);
Kavie.addVariableValidation("dynamicSection", self.validate);

self.submit = function(){
  if (Kavie.isValid("dynamicSection") { 
    console.log("All Good!");
  }
}

Section Children

If you want to chain sections together you can do that as well

self.value = ko.observalbe().extend({
  kavie: {
    section: "parent"
  }
});

self.value2 = ko.observalbe().extend({
  kavie: {
    section: "child"
  }
});

Kavie.addSectionChild("parent", "child");

self.submit = function(){
  if (Kavie.isValid("parent") { 
    // both parent and child are valid
    console.log("All Good!");
  }
}