Reputation: 12173
I'm working on a Durandal JS SPA application, and I wish to use Knockout Validation.
The problem is that validation is being triggered on page load, which is not what I want - I'd like to trigger the validation myself.
I have tried using
ko.validation.init({
registerExtenders: true,
messagesOnModified: true,
insertMessages: false
});
as well as ko.validation.configure with the same parameters, followed by ko.validation.init();
Here is a snippet of my viewmodel.
function ViewModel(){
var self = this;
self.username = ko.observable().extend({
required: true
});
self.errors = ko.validation.group(self);
}
I also tried delaying the call to ko.validation.group(self) till a button is clicked, but then it wont validate at all.
Upvotes: 0
Views: 1227
Reputation: 2853
assuming you only want to show a summary of validation errors per some action, you could do something like this:
html
<input type="text" data-bind='value: username' />
<br/>
<button data-bind="click: submit">Submit</button>
<div data-bind="visible: showErrors, text: errors" />
js
function ViewModel() {
var self = this;
self.username = ko.observable().extend({
required: true
});
self.showErrors = ko.observable(false);
self.submit = function () {
self.showErrors(true);
if (self.isValid()) {
// save data here
}
}
self.errors = ko.validation.group(self);
}
sample fiddle
Upvotes: 1