Reputation: 1183
I have 4 fields, each have their own custom validation but non are required. How would I put a validation so that at least one of the field must be filled.
self = this;
self.acc = ko.observable().extend({ acc: true });
self.Id = ko.observable().extend({ id: true });
self.Name= ko.observable().extend({ name: true });
self.Number = ko.observable().extend({ number: true});
I know that there is onlyIf
for the required
extender but im not sure if that is the correct way to handle this type of validation.
Upvotes: 2
Views: 485
Reputation: 2258
In a situation like this, I just make a separate computed that does special validation like that. This computed can be used to enable/disable the ability to submit a form, or what have you.
self.atLeastOneFilled = ko.computed(function(){
// note: number will need a different validation if zero is allowed (null check, for example)
return self.acc() || self.Id() || self.Name() || self.Number();
});
Upvotes: 2