使用多步向导样式模型进行ko.validation

时间:2013-07-17 16:50:12

标签: knockout.js knockout-validation

我正在尝试将this answer转换为使用ko.validation。

我只是在模型状态有效时才停下来启用下一个按钮,但是下面的代码没有正确评估:

self.modelIsValid = function() {
        self.currentStep().model().isValid();
};

我目前卡住了,并希望看到一些新鲜的眼睛。该模型确实验证了OK(显示了错误消息),最后一步是使用控制下一个按钮的modelIsValid验证每个模型。

jsfiddle is here,以及下面的代码。

ko.validation.configure({
    insertMessages: false,
    decorateElement: true,
    errorElementClass: 'error'
});

function Step(id, name, template, model) {
    var self = this;
    self.id = id;
    self.name = ko.observable(name);
    self.template = template;
    self.model = ko.observable(model);

    self.getTemplate = function() {
        return self.template;
    };
}

function ViewModel(model) {
    var self = this;

    self.nameModel = ko.observable(new NameModel(model));    
    self.addressModel = ko.observable(new AddressModel(model));

    self.stepModels = ko.observableArray([
            new Step(1, "Step1", "nameTmpl", self.nameModel()),
            new Step(2, "Step2", "addressTmpl", self.addressModel()),
            new Step(3, "Confirmation", "confirmTmpl", {NameModel: self.nameModel(), AddressModel:self.addressModel()})]);

    self.currentStep = ko.observable(self.stepModels()[0]);

    self.currentIndex = ko.computed(function() {
        return self.stepModels.indexOf(self.currentStep());
    });

    self.getTemplate = function(data) {
        return self.currentStep().template();
    };

    self.canGoNext = ko.computed(function() {
        return (self.currentIndex() < (self.stepModels().length - 1));
    });    

    self.modelIsValid = function() {
        self.currentStep().model().isValid();
    };

    self.goNext = function() {
        if (((self.currentIndex() < self.stepModels().length - 1) && ($('.validationMessage:visible').length <= 0))) {
            self.currentStep(self.stepModels()[self.currentIndex() + 1]);
        }
    };

    self.canGoPrevious = ko.computed(function() {
        return self.currentIndex() > 0;
    });

    self.goPrevious = function() {
        if ((self.currentIndex() > 0 && ($('.validationMessage:visible').length <= 0))) {
            self.currentStep(self.stepModels()[self.currentIndex() - 1]);
        }
    };
}

NameModel = function (model) {
    var self = this;

    //Observables
    self.FirstName = ko.observable(model.FirstName).extend({ required: true });
    self.LastName = ko.observable(model.LastName).extend({ required: true });

    return self;
};

AddressModel = function(model) {
    var self = this;

    //Observables
    self.Address = ko.observable(model.Address).extend({ required: true });;
    self.PostalCode = ko.observable(model.PostalCode).extend({ required: true });;
    self.City = ko.observable(model.City).extend({ required: true });;

    return self;
};

     var viewModelFromServer = {
        "FirstName": "John",
        "LastName": "Doe",
        "Address": "123 Main St",
        "PostalCode": "53201",
        "City": "Milwaukee"
    };

ko.applyBindings(new ViewModel(viewModelFromServer));

编辑凯文的回答

最终视图没有显示,结果是因为没有为视图定义验证组,所以将modelsIsValid函数更改为以下内容:

self.modelIsValid = ko.computed(function () {
    if (typeof(self.currentStep().model().isValid) != "undefined") { 
        return self.currentStep().model().isValid();
    }
    else
        return true; // no validation used for viewmodel, so just return true
});

这似乎有用,虽然我非常喜欢javascript新手!

1 个答案:

答案 0 :(得分:2)

您已将验证规则应用于“名称”和“地址”模型中的各个可观察对象,但您尝试使用分组的功能而不在每个模型中明确地进行分组:

NameModel = function (model) {
    var self = this;

    //Observables
    self.FirstName = ko.observable(model.FirstName).extend({ required: true });
    self.LastName = ko.observable(model.LastName).extend({ required: true });

    ko.validation.group(self);

    return self;
};

另外,您需要将modelIsValid作为计算的observable:

self.modelIsValid = ko.computed(function() {
    return self.currentStep().model().isValid();
});

更新小提琴 - http://jsfiddle.net/jnTjW/4/