Knockout Validation disable validation
Asked Answered
A

1

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);
Anaya answered 15/11, 2012 at 14:54 Comment(4)
Please include your current code.Camporee
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.Anaya
You should write self.name.extend({ validatable: !self.deleted()}); in your deleteMe function.Laise
Dang! Oh course. So that kind-of works. It looks like this method will disable the validation, but it won't re-enable. jsfiddle.net/3RZjT/26Anaya
A
12

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
Aye answered 15/11, 2012 at 15:29 Comment(8)
It didn't work. I've updated my question with an example and jsFiddle.Anaya
File a ticket, it should work github.com/ericmbarnard/Knockout-Validation/issues/101Aye
Opps, a bug in my code. If fixed it and it will disable now, but it won't re-enable.Anaya
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.Anaya
I need this feature to so I opened a ticket github.com/ericmbarnard/Knockout-Validation/issues/171Aye
I found a pull request that adds enable/disable functionality: github.com/ericmbarnard/Knockout-Validation/pull/127Anaya
The onlyIf option works well. Thanks!Anaya
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.Canonry

© 2022 - 2024 — McMap. All rights reserved.