Knockout Validation disable validation
10,575
Update Old answer not valid for the Knockout-Contrib version of Validation (Thats the branch with active development)
https://github.com/Knockout-Contrib/Knockout-Validation
Use the onlyIf option like
this.enable = ko.observable(true);
this.required = ko.observable().extend({ required: { onlyIf: this.enable } });
Old answer
Try
this.property.extend({ validatable: false }); // disables all validation for property
or
this.property.extend({ required: false }); // disables required validation for property
Author by
Homer
Updated on July 18, 2022Comments
-
Homer almost 2 years
I have ko.observableArrays with validation on the items. The user can mark a item as deleted. When it is marked as deleted, I need to disable validation on that item.
How do you dynamically disable validation?
Example: http://jsfiddle.net/3RZjT/2/
<div data-bind="foreach: names"> <input data-bind="value: name, valueUpdate: 'afterkeydown'" /> <a data-bind="click: deleteMe, text:deleted()?'undelete':'delete'" href="#">delete</a><br/> </div> function Person(name){ var self = this; self.name = ko.observable(name).extend({ required: true}); self.deleted = ko.observable(false); self.deleteMe = function(){ self.deleted(!self.deleted()); self.deleted.extend({ validatable: !self.deleted()}); }; } var viewModel = { names: ko.observableArray([new Person("Ken"), new Person("")]) }; ko.applyBindings(viewModel);
-
Homer over 11 yearsIt didn't work. I've updated my question with an example and jsFiddle.
-
Anders over 11 yearsFile a ticket, it should work github.com/ericmbarnard/Knockout-Validation/issues/101
-
Homer over 11 yearsOpps, a bug in my code. If fixed it and it will disable now, but it won't re-enable.
-
Homer over 11 yearsI'm marking this as the answer because, well, it is the answer to the question "How do you dynamically disable validation?". But, for my purposes, I also need to be able to re-enable validation. Looking at the source, it looks like enable/disable is not supported by
validatable: false/true
. When you use.extend({ validatable: false })
, it deletes all the validation rules. At that point there is nothing to re-enable. -
Anders over 11 yearsI need this feature to so I opened a ticket github.com/ericmbarnard/Knockout-Validation/issues/171
-
Homer over 11 yearsI found a pull request that adds enable/disable functionality: github.com/ericmbarnard/Knockout-Validation/pull/127
-
Homer almost 11 yearsThe onlyIf option works well. Thanks!
-
Slight almost 9 yearsUsing
onlyIf
is not a good solution because it must be applied to each validation rule on an observable. Why have this redundant code when you have the option of flipping an on/off switch for the entire observable? Usevalidatable: false
and use a function to register extensions if you need to reenable them again. People act likeonlyIf
is the end-all solution for a lot of things when it's not because it can't be applied to the entire observable itself, only individual rules which is bad design to say the least.