KnockoutJs验证Async isValidating始终为true

时间:2015-08-06 03:38:26

标签: javascript knockout.js

在高级别,我试图在异步验证完成之前阻止表单提交,并确保在提交表单之前所有内容都有效...

查看模型摘录:

 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;
        }

我的问题是self.isValidating始终为真,我从这个StackOverflow问题的答案中得到了这段代码:Knockout Validation async validators: Is this a bug or am I doing something wrong?

唯一的区别是我的saveData函数不是selfthis viewModel的一部分,就像答案示例一样,但我不认为这很重要。

为什么isValidating总是如此?

0 个答案:

没有答案