My goal is to apply a knockout required validator on an input that is part of a collection on a viewmodel and made visible inside a foreach binding. Here's what I have so far - errors always evals to 0, not sure where I'm going wrong here.
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' }" />