I have a viewmodel and there I have properties which are extended to use validation. I call ko.validation.group(self) but this doesn't add the isValid() method to the viewmodel.
So I get an error that isValid() is undefined.
Here is my code:
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);
I have created a js fiddle: click here
I've read somewhere that you need to call registerExtenders() but I tried it and it doesn't work either.
Can someone help me in the right direction? Thx!