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
Share:
10,575
Homer
Author by

Homer

Updated on July 18, 2022

Comments

  • Homer
    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
    Homer over 11 years
    It didn't work. I've updated my question with an example and jsFiddle.
  • Anders
    Anders over 11 years
  • Homer
    Homer over 11 years
    Opps, a bug in my code. If fixed it and it will disable now, but it won't re-enable.
  • Homer
    Homer over 11 years
    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.
  • Anders
    Anders over 11 years
    I need this feature to so I opened a ticket github.com/ericmbarnard/Knockout-Validation/issues/171
  • Homer
    Homer over 11 years
    I found a pull request that adds enable/disable functionality: github.com/ericmbarnard/Knockout-Validation/pull/127
  • Homer
    Homer almost 11 years
    The onlyIf option works well. Thanks!
  • Slight
    Slight almost 9 years
    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.