我的目标是在作为视图模型集合的一部分并在 foreach 绑定中可见的输入上应用敲除所需的验证器。到目前为止,这是我所拥有的 - 错误总是评估为 0,不确定我在哪里出错了。
var vm = {
myCollection: myCollection, <-- im binding the foreach onto this property, and consists of a collection of Items
submit: function () {
if (vm.errors().length == 0) {
alert('Thank you.'); <-- errors is always 0
} else {
vm.errors.showAllMessages();
app.showMessage('There were some errors...', '');
}
}
};
var Item = function (data) {
self.name = ko.observable().extend({ required: true });
}
vm["errors"] = ko.validation.group(vm);
<div data-bind="foreach: myCollection">
<input type="text"
data-bind="value: name,
validationOptions: {errorElementClass: 'input-validation-error' }" />