我有ko.observableArrays,对项目进行了验证。用户可以将项目标记为已删除。当它被标记为已删除时,我需要禁用该项目的验证。
如何动态禁用验证?
示例: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);
答案 0 :(得分:11)
<强>更新强> 旧答案对于验证的Knockout-Contrib版本无效(这是具有活动开发的分支)
https://github.com/Knockout-Contrib/Knockout-Validation
使用onlyIf选项,如
this.enable = ko.observable(true);
this.required = ko.observable().extend({ required: { onlyIf: this.enable } });
旧答案
尝试
this.property.extend({ validatable: false }); // disables all validation for property
或
this.property.extend({ required: false }); // disables required validation for property