-
Notifications
You must be signed in to change notification settings - Fork 379
Validating Nested Values
Ian Strachan edited this page Oct 27, 2015
·
2 revisions
Sometimes you will need to validate an object that contains a collection of other objects that must also be validated. In this case you will need to configure Knockout-Validation to use deep grouping to recursively walk the view model.
ko.validation.init({
grouping: {
deep: true,
live: true,
observable: true
}
});
You can then reference observable arrays from within your validation groups and the nested objects will be validated.
User = function () {
var self = this;
// observables
self.firstName = ko.observable();
// validation
self.validationObject = ko.validatedObservable({
firstName: self.firstName.extend({
required: true
})
});
};
IndexViewModel = function () {
var self = this;
// observables
self.users = ko.observableArray();
self.name = ko.observable();
// functions
self.addUser = function () {
self.users.push(new User());
};
// validation
self.validationObject = ko.validatedObservable({
users: self.users,
name: self.name.extend({required: true})
});
};
A complete example can be found here.