淘汰赛验证长度始终为0 [英] Knockout Validation length is always 0

查看:123
本文介绍了淘汰赛验证长度始终为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用淘汰赛是陌生的,我正在尝试使验证插件正常工作.但是,我尝试了ViewModel.errors().length == 0,但始终为零 当我检查isValid时-我总是正确的.

I am new to using knockout and I am trying to get the validation plug-in to work. However, I tried ViewModel.errors().length == 0 but it is always zero when i check the isValid - I always get true.

这是我剩下的代码,请帮忙.

Here is the rest of my code, please help.

define(['knockout','knockout-validation', 'services/changeup', 'services/currencies', 'plugins/router'], function (ko, validation, changeup, currencies, router) {

ko.validation.configure({
    insertMessages: true,
    decorateElement: true,
    errorElementClass: 'error',
    errorMessageClass: 'help-inline '
});

var ctor = function () {

    this.amount = ko.observable().extend({ required: true, number: true});
    this.currency = ko.observable().extend({ required: true});
    this.requestedAmount = ko.observable();
    this.requestedCurrency = ko.observable().extend({ required: true, notEqual: this.currency, message: 'please'});
    this.comment = ko.observable().extend({ required: true, minLength: 3});
    this.currencies = currencies;
};
ctor.errors = ko.validation.group(ctor);

ctor.prototype.activate = function (activationData) {
};

ctor.prototype.save = function () {


    var valid = ctor.isValid();
    console.log(valid);

    if (ctor.isValid()){
        ctor.errors.showAllMessages();
    }
    else {
        var dto = ko.toJS(this);
        delete dto.currencies;
        changeup.createRequest(dto).then(function(request){
            console.log(request, 'a');
            router.navigate('dashboard');
        });
    }
};

ctor.prototype.cancel = function (activationData) {
};

return ctor;

});

推荐答案

ko验证组不应与此函数本身一起附加,因此您的代码将类似于:-

ko validation group should be attached with this not with the function itself so your code wil be like :-

var ctor = function () {

 this.amount = ko.observable().extend({ required: true, number: true});
 this.currency = ko.observable().extend({ required: true});
 this.requestedAmount = ko.observable();
 this.requestedCurrency = ko.observable().extend({ required: true, notEqual:  this.currency, message: 'please'});
 this.comment = ko.observable().extend({ required: true, minLength: 3});
 // this.currencies = currencies;
 this.errors = ko.validation.group(this);
};

保存功能将是:-

ctor.prototype.save = function () {
 var valid = this.isValid();
 console.log(valid);

 if (!this.isValid()){  //use this 
    this.errors.showAllMessages();
 }
 else {
    var dto = ko.toJS(this);
    delete dto.currencies;
    changeup.createRequest(dto).then(function(request){
        console.log(request, 'a');
        router.navigate('dashboard');
    });
 }
};

小提琴演示

这篇关于淘汰赛验证长度始终为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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