通常,验证与各自的绑定内容无关,例如http://jsfiddle.net/supercool/JL26Z/53/
但我有两个文本框name1
& name2
,如小提琴中提到的,如果以下条件失败,我需要执行show验证失败:
name1
,则必须name2
。name2
,则必须name1
。name1
& name2
留空我需要显示错误消息,即验证失败。如果我让你感到困惑:简而言之,如果在任何文本框中输入文字,我都希望通过验证。
答案 0 :(得分:1)
像@delixfe suggested in another question here on SO:
一样var Phone = function () {
var self = this;
self.name1 = ko.observable("");
self.name2 = ko.observable("");
self.numbersHaveNoValue = ko.computed(function () {
var value1 = self.name1(), value2 = self.name2();
return !ko.validation.rules.required.validator(value1, true) &&
!ko.validation.rules.required.validator(value2, true);
});
self.Validation = ko.validatedObservable([
self.name1.extend({ required: { onlyIf: self.numbersHaveNoValue }}),
self.name2.extend({ required: { onlyIf: self.numbersHaveNoValue }})
]);
}
请参阅fiddle