Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove extender from an existing observable?

Tags:

I am using the Knockout Validation plugin and setting an observable as required using the extender:

myObservable.extend({required:true});

Is it possible for me to remove the extender after adding it?

like image 501
Jonas Stawski Avatar asked Jun 15 '13 05:06

Jonas Stawski


2 Answers

You can remove all the validation relates properties form an observable which were added by the ko validation with calling:

myObservable.extend({validatable: false});

Or if you want to only remove the required validation you can remove it from the rules collection:

myObservable.rules.remove(function (item) {
        return item.rule == "required";
    });
}

Demo JSFiddle.

But the ko validation has support for conditional validation, so you can specify some condition when the validation should work so maybe this is what you need:

myObservable.extend({
    required: {
        message: "Some message",
        onlyIf: function () { return //some condition; }
    }
});
like image 148
nemesv Avatar answered Sep 22 '22 21:09

nemesv


nemesv answer works with a small typo correction - the function in the remove(...) call should return a boolean value (i.e. '==' instead of '='):

myObservable.rules.remove(function(item) {
  return item.rule == "required";
});

Demo: JSFiddle

like image 38
mikemsq Avatar answered Sep 20 '22 21:09

mikemsq