gpt4 book ai didi

validation - knockout 验证 isValid() 不起作用

转载 作者:行者123 更新时间:2023-12-02 09:30:48 25 4
gpt4 key购买 nike

我有一个 View 模型,其中有扩展为使用验证的属性。我调用 ko.validation.group(self) 但这不会将 isValid() 方法添加到 View 模型中。

所以我收到了 isValid() 未定义的错误。

这是我的代码:

var brechtbaekelandt = brechtbaekelandt || {};

brechtbaekelandt.login = (function ($, jQuery, ko, undefined) {
"use strict";

function LoginViewModel() {

var self = this;

self.userName = ko.observable();
self.password = ko.observable();
self.rememberMe = ko.observable();

self.errorMessage = ko.observable();

self.userName.extend({ required: { message: 'Please enter your username' } });
self.password.extend({ required: { message: 'Please enter your password' } });

self.errors = ko.validation.group(self);
};

LoginViewModel.prototype.login = function () {

var self = this;

self.errorMessage(null);

alert('entering login');

// self.isValid() is not a function
if (!self.isValid()) {

alert('login invalid');

self.errors.showAllMessages();
return;
}
else
{
alert('login valid');
// do login
}
};

function init() {

alert('entering init');

var knockoutValidationSettings = {
insertMessages: false,
decorateElement: true,
decorateElementOnModified: true,
decorateInputElement: true,
//errorMessageClass: 'error',
//errorElementClass: 'error',
//errorClass: 'error',
errorsAsTitle: false,
parseInputAttributes: false,
messagesOnModified: true,
messageTemplate: null,
grouping: { deep: true, observable: true }
};

ko.validation.init(knockoutValidationSettings, true);

var viewModel = new LoginViewModel();

ko.applyBindingsWithValidation(viewModel);
}

return {
LoginViewModel: LoginViewModel,
init: init
};

})($, jQuery, ko);

我创建了一个js fiddle :click here

我在某处读到过,您需要调用 registerExtenders(),但我尝试过,但它也不起作用。

有人可以帮助我朝正确的方向前进吗?谢谢!

最佳答案

好吧,在使用 group 时,您似乎正在寻找 isValid ,但有一种方法(替代方法)使用 length 属性来实现它。由于使用 group 时 isValid 似乎不可用(它与 validObservable 一起存在)。

正如 @jeff 在该主题的一个答案中提到的

The ko.validation.group just gives you an (computed) observable of all the error messages in a model. It only collects error messages of direct properties of the model.

The ko.validatedObservable on the other hand not only collects the error messages, but also wraps the model in an observable and adds an isValid property which indicates whether or not there are any error messages (i.e., the model was completely valid). Otherwise, they're essentially the same.

我相应地修改了您的代码,如下所示

    self.errors = ko.validation.group(self); //It will group error messages in array i.e based on count you must validate 
LoginViewModel.prototype.login = function () {
var self = this;
self.errorMessage(null);
//self.isValid() doesn't exist here . so you should base length
if (self.errors().length>0) {
alert('login invalid');
self.errors.showAllMessages();
return;
}
};

工作示例 group

使用 ValidatedObservable 的工作示例 更好的方式恕我直言

关于validation - knockout 验证 isValid() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33235883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com