0

我将验证库 [Knockout Validation][1] 与 Knockout 2 一起使用。虽然 UI 正确显示对象数组内的可观察属性的验证错误。ko.validation.group 函数不跟踪这些错误。查看源代码,我看到它只检查数组对象本身的可观察性,而不是内部可观察属性。

我有一个数组

function Game(bowlerId, name, g1, g2, g3, g4) {
    this.BowlerId = bowlerId;
    this.Name = name;
    this.Game1 = ko.observable(g1).extend({ required: true, min:1, max:300  });
    this.Game2 = ko.observable(g2).extend({ required: true, min: 1, max: 300 });
    this.Game3 = ko.observable(g3).extend({ required: true, min: 1, max: 300 });
    this.Game4 = ko.observable(g4).extend({ required: true, min: 1, max: 300 });

}

我用的是:

viewModel.errors = ko.validation.group(viewModel.Games);

或喜欢

viewModel.errors = ko.validation.group(viewModel);

在任何一种情况下,上述单个属性(Game1、Game2 等)都不会在 group 函数返回的错误中进行跟踪。验证错误确实出现在 UI 中。目前我必须查询 DOM 以查看用户是否提出了验证错误。有什么办法可以让它工作吗?

4

1 回答 1

0

你有没有尝试过?

viewModel.errors = ko.validation.group(viewModel.Games, { deep: true });
于 2012-11-06T16:39:12.177 回答