我用ko.validation检查有效数据我的网页上是这样的:ko.validation与validatedObservable让我奇怪的结果
var postcode = ko.observable(),
name = ko.observable();
var validationModel = ko.validatedObservable({
postcode: postcode.extend({ required: true }),
name: name.extend({ required: true })
});
然后在我的OK按钮提交我前检查验证:
var buttonOk = function() {
if (!validationModel.isValid()) {
validationModel.errors.showAllMessages();
return false;
}
...
它工作得很好:如果用户没有输入邮编的内容并且名称验证失败。
现在我加入了一些更验证规则:
postcodeMustNotAlreadyExists + denominationMustNotAlreadyExists这样的:
var validationModel = ko.validatedObservable({
postcode: postcode.extend({ required: true }),
name: name.extend({ required: true })
}).extend({
postcodeMustNotAlreadyExists: cities,
denominationMustNotAlreadyExists: cities
});
ko.validation.rules['postcodeMustNotAlreadyExists'] = {
validator: function (val, cities) {
// Try to find a match between the typed postcode and the postcode in the list of cities
var match = ko.utils.arrayFirst(cities(), function (item) {
return (val.postcode() === item.postCode());
});
return !match;
},
message: 'This postcode already exists!'
};
ko.validation.rules['denominationMustNotAlreadyExists'] = {
validator: function (val, cities) {
// Try to find a match between the typed denomination and the denomination in the list of cities
var match = ko.utils.arrayFirst(cities(), function (item) {
return (val.name() === item.name());
});
return !match;
},
message: 'This denomination already exists!'
};
ko.validation.registerExtenders();
现在validationModel.isValid()
回报总是如此,当用户不键入邮政编码或名称什么。我注意到validationModel().postcode.isValid()
是错误的,因此这不是逻辑将validationModel.isValid()设置为True。
现在与我的新的实施一定要考2两件事:(!validationModel.isValid() || validationModel().errors().length>0)
任何想法?
谢谢。
我不得不深入false,因为它给我一个堆栈溢出..但是这个覆盖似乎是我需要myModel.isValid()开始工作。在文档中它似乎并不需要? –