簡體   English   中英

淘汰賽驗證長度始終為0

[英]Knockout Validation length is always 0

我是使用淘汰賽的新手,並且正在嘗試使驗證插件正常工作。 但是,我嘗試了ViewModel.errors()。length == 0,但是當我檢查isValid時它始終為零-我總是得到true。

這是我其余的代碼,請幫助。

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驗證組應該與此附加在一起,而不是與函數本身相連,因此您的代碼將類似於:-

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

小提琴演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM