如何从现有的可观察物中移除增量剂?

问题描述:

我正在使用Knockout Validation插件,并使用扩展程序根据需要设置可观察对象:

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?

您可以从ko验证通过调用以下方法删除所有可验证的相关属性:

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

myObservable.extend({validatable: false});

或者,如果您只想删除所需的验证,则可以将其从rules集合中删除:

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";
    });
}

演示 JSFiddle .

但是ko验证支持条件验证,因此您可以在验证有效时指定一些条件,也许这就是您所需要的:

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; }
    }
});