10

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
  • 7,594
  • 14
  • 69
  • 109
  • 1
    Please include your current code. – Tomalak Nov 15 '12 at 14:57
  • I don't really have an attempt because I don't know what to try. I've updated with an example using @Anders answer, but it didn't work for me. – Homer Nov 15 '12 at 15:42
  • You should write `self.name.extend({ validatable: !self.deleted()});` in your `deleteMe` function. – nemesv Nov 16 '12 at 07:24
  • Dang! Oh course. So that kind-of works. It looks like this method will disable the validation, but it won't re-enable. http://jsfiddle.net/3RZjT/26/ – Homer Nov 16 '12 at 14:07

1 Answers1

11

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
Anders
  • 17,306
  • 10
  • 76
  • 144
  • It didn't work. I've updated my question with an example and jsFiddle. – Homer Nov 15 '12 at 15:42
  • File a ticket, it should work https://github.com/ericmbarnard/Knockout-Validation/issues/101 – Anders Nov 15 '12 at 17:36
  • Opps, a bug in my code. If fixed it and it will disable now, but it won't re-enable. – Homer Nov 16 '12 at 14:08
  • 3
    I'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. – Homer Nov 16 '12 at 15:18
  • I need this feature to so I opened a ticket https://github.com/ericmbarnard/Knockout-Validation/issues/171 – Anders Nov 16 '12 at 19:10
  • 1
    I found a pull request that adds enable/disable functionality: https://github.com/ericmbarnard/Knockout-Validation/pull/127 – Homer Nov 16 '12 at 19:22
  • The [onlyIf](https://github.com/Knockout-Contrib/Knockout-Validation/wiki/Conditional-Validation-with-onlyIf-parameter) option works well. Thanks! – Homer Jul 15 '13 at 18:41
  • 1
    Using `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? Use `validatable: false` and use a function to register extensions if you need to reenable them again. People act like `onlyIf` 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. – Slight Jun 30 '15 at 13:56