【发布时间】:2014-09-08 01:37:36
【问题描述】:
我无法弄清楚如何让Knockout Validation plugin 验证视图模型属性的自定义选择。 我可以致电isValid() 来成功验证整个视图模型。
我遵循了here 中的文档,其中涵盖了该场景,并检查了我在堆栈溢出时可以找到的所有答案。
我的代码如下所示:
function MyViewModel() {
var self = this;
self.myproperty = ko.observableArray().extend({ minLength: { message: 'You must specify at least one item.'} })
self.anotherproperty = ko.observable().extend({ required: { params: true, message: 'You must supply a value.'} });
self.IsEntireModelValid = function() {
if (!self.isValid()) {
self.errors.showAllMessages();
return false;
}
else {
return true;
}
self.IsAnotherPropertyValidOnly = function() {
var errors = ko.validation.group(self.anotherproperty);
if (errors.length > 0) {
errors.showAllMessages();
return false;
}
else {
return true;
}
}
当我调用self.IsAnotherPropertyValidOnly() 时,errors 变量不包含任何错误,但是当我调用self.IsEntireModelValid() 时,我得到了正确的响应。
谁能指出我做错了什么?
【问题讨论】:
标签: javascript validation knockout.js knockout-validation