0

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?

4
  • Have you called ko.validation.init? Aug 6, 2015 at 3:55
  • How is self.Name defined?
    – Roy J
    Aug 6, 2015 at 10:39
  • self.Name is just a ko.observable, I am using ko.mapping.fromJS to "auto" create observables from the json model from the server Aug 6, 2015 at 21:02
  • I do not call ko.validation.init, do I need do, my validation does work and fires off validation errors Aug 6, 2015 at 21:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.