使用Knockout验证插件时,Knockout ViewModel isValid错误 [英] Knockout ViewModel isValid error when using Knockout validation plugin

查看:135
本文介绍了使用Knockout验证插件时,Knockout ViewModel isValid错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用淘汰赛的新手,我正在尝试使用淘汰验证插件以及映射插件。我在视图模型对象上遇到了isValid函数的问题。根据文档,isValid应返回一个bool来确定视图模型上的任何可观察对象是否有效,但是当我调用它时,我得到一个错误,说isValid不是函数。但是,如果我在observables上调用isValid它可以正常工作。问题是我正在使用映射插件和我从服务器获得的一些动态数据对象,因此我不一定知道我需要验证的observable的名称,因此单独验证它们是不可行的。以下示例已经过简化,但在实际实现中,我不知道可观察对象的名称。也许我刚刚错过了一些文档?

I'm new to using knockout and I'm trying to use the knockout validation plugin along with the mapping plugin. I'm having an issue with the isValid function on the view model object. According to the documentation isValid should return a bool to determine whether any of the observables on the view model are in valid but when I call it I get an error saying that isValid is not a function. However if I call isValid on the observables themselves it works fine. The problem is I'm using the mapping plugin with some dynamic data objects that I get from the server so I don't necessarily know the names of the observables I need to validate so it's not feasible to validate them individually. The example below is simplified but in the real implementation I don't know the names of the observables. Maybe I've just missed some documentation?

感谢您的时间。

这确实有效

    var dataItem = { FirstName: '', LastName: '', Age: '', Email: '' }


    var viewModel = function(data) {

        var self = this;
        this.Email = ko.observable().extend({ email: true });
        this.LastName = ko.observable().extend({ required: true });
        this.Age = ko.observable().extend({ required: true, min: 0 });
        this.saveClick = function () {

            if (!self.Email.isValid() || !self.Age.isValid() || !self.LastName.isValid()) {
                alert('Not valid');
            else {

                alert('Valid');
            }
        };
        this.cancelClick = function () {

            ko.mapping.fromJS(dataItem, null, this);
        }

        ko.mapping.fromJS(data, {}, this);
    };

    var viewModelInstance = new viewModel(dataItem);
    ko.applyBindings(viewModelInstance, document.getElementById('bindingDiv'));

但这不起作用

        var dataItem = { FirstName: '', LastName: '', Age: '', Email: '' }


        var viewModel = function(data) {

            var self = this;
            this.Email = ko.observable().extend({ email: true });
            this.LastName = ko.observable().extend({ required: true });

            this.Age = ko.observable().extend({ required: true, min: 0 });
            this.saveClick = function () {
                //TODO: according to the documentation you should be able to just
                //check self.isValid() but that throws an error that there is no
                //such method on the object? dunno why.
                if (!self.isValid()) {
                    alert('Not Valid');
                }
                else {
                    alert('Valid');
                }
            };
            this.cancelClick = function () {

                ko.mapping.fromJS(dataItem, null, this);
            }

            ko.mapping.fromJS(data, {}, this);
        };

        var viewModelInstance = new viewModel(dataItem);
        ko.applyBindings(viewModelInstance, document.getElementById('bindingDiv'));


推荐答案

致电 ko.validation。在VM中对group 进行分组,以便在VM级别对所有可验证的可观察对象进行分组。然后 isValid 只有在没有子观察者有错误时才会为真。

Call ko.validation.group in your VM to group all the validatable observables at the VM level. Then isValid will be true only if no child observables have errors.

关于ko.validation的其他一些SO答案。小组

Some other SO answers about ko.validation.group

如何使用ko.validation.group函数

Knockout Validation ko.validation.group vs ko.validatedObservable

这篇关于使用Knockout验证插件时,Knockout ViewModel isValid错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆