At a high level, I am trying to prevent form submission before async validation is complete and also ensure all is valid before submitting the form...
View Model Excerpt:
ko.validation.rules['projectNameValidator'] = {
async: true,
validator: function (val, otherVal, callback) {
if (self.ProjectId && self.ProjectId() > 0 && val === self.OriginalProjectName())
return true;
$.post("/ValidationBuild/IsProjectNameAvailable?name=" + val, function (data) {
callback(data);
});
}
};
ko.validation.registerExtenders();
self.Name.extend({
required: { message: 'Please Provide a project name' },
projectNameValidator: { message: 'You have already used this project name, please use a unique project name' }
});
self.isValidating = ko.computed(function () {
return self.Name.isValidating();
});
self.errors = ko.validation.group(self);
self.formSubmit = saveData;
return self;
function saveData(data, event) {
if (self.isValidating()) {
setTimeout(function () {
saveData(data, event);
}, 50);
return false;
}
// ko.validation check if valid
if (self.errors().length > 0) {
self.errors.showAllMessages(true);
return;
}
My problem is that self.isValidating
is always true, I got this code from the answer to this StackOverflow question here: Knockout Validation async validators: Is this a bug or am I doing something wrong?
The only difference is that my saveData function is not part of the self
or this
viewModel like the answer example is but I don't think that matters.
Why is isValidating
always true?