I'm using Knockout for a View bound to a ViewModel with some depth/complexity to it.
To validate my ViewModel i need to compute across the entire VM tree (walking & comparing descendants). I was hoping to leverage the knockout-validation plugin @ https://github.com/ericmbarnard/Knockout-Validation to create some arbitrary custom validation rule and report any errors.
I set up one possible approach using a KO computed observable @ http://jsfiddle.net/drdamour/ZrVZ7/ . The validation rule is to make sure no 2 SubCollections have the same value. This is working, but it's not using Knockout-Validation and my rule is now implemented in my computed observable which seems wrong.
My question is very similar to Knockout Validation validatedObservable group error but i find the discovered solution (hosted at http://jsfiddle.net/CGuW2/6/) to be less than ideal:
var viewModel = {
num1: ko.observable("50").extend({ number: true, min: 0, max: 100 }),
num2: ko.observable("50").extend({ number: true, min: 0, max: 100 })
};
viewModel.isValidSum = ko.validatedObservable({
num1: viewModel.num1,
num2: viewModel.num2
}).extend({ mustEqual: 100 });
ko.applyBindings(viewModel);
as it's duplicating the ViewModel by adding it's validated self as a property to itself (isValidSum). I tried to eliminate this redundancy @ http://jsfiddle.net/drdamour/5B8s4/ but the view fails to bind to the validatedObservable:
var viewModel = {
num1: ko.observable("50").extend({ number: true, min: 0, max: 100 }),
num2: ko.observable("50").extend({ number: true, min: 0, max: 100 })
};
var vm = ko.validatedObservable(viewModel).extend({ mustEqual: 101 });
ko.applyBindings(vm);
This may be a bug with KO or KO-Validation, or i may just be doing it totally wrong.
So the question is: what's the best way in knockout to determine if a VM is valid against a condition that evaluates across multiple properties & layers of the VM?