规则定义为:
self.state = ko.observable().extend({
required: {
onlyIf: function () {
return self.country() === 'US';
}
}
});
似乎执行state.rules()[0].condition()
会告诉您规则是否处于活动状态,但感觉不是正确的方法。
以下是一个工作示例:http://jsfiddle.net/LYP5u/64/
答案 0 :(得分:0)
如果您的验证规则为true,则您的规则处于有效状态,因此您可以尝试此解决方案:
ko.validation.init();
function Address() {
var self = this;
self.availableCountries= ['AU', 'NZ', 'US'];
self.availableStates = ['Alabama', 'Alaska', 'Arizona', 'Arkansas'];
self.country = ko.observable().extend({required: true});
self.stateRule = function(){
return self.country() === 'US';
}
self.state = ko.observable().extend({
required: {
onlyIf: self.stateRule
}
});
self.errors = ko.validation.group(self);
};
var address = ko.validatedObservable(new Address())();
ko.applyBindings(address);
使用此模板进行测试:
<p data-bind="text: stateRule()"><p>
<p data-bind="text: state.rules()[0].condition()"><p>
<div>
<label for="country">Country</label>
<select id="country" data-bind="options: availableCountries, value: country, optionsCaption: 'Choose...'"></select>
<div data-bind="visible: state.rules()[0].condition()">
<label for="state">State</label>
<select id="state" data-bind="options: availableStates, value: state, optionsCaption: 'Choose...'"></select>
</div>
</div>