Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't configure knockout validation

I'm trying to configure knockout validation, but my settings seem to be getting ignored. This is how I'm trying to configure it

var knockoutValidationSettings = {
    insertMessages: true,
    decorateElement: true,
    errorMessageClass: 'error',
    errorElementClass: 'error',
    errorClass: 'error',
    errorsAsTitle: true,
    parseInputAttributes: false,
    messagesOnModified: true,
    decorateElementOnModified: true,
    decorateInputElement: true
};
ko.validation.init(knockoutValidationSettings);

ko.applyBindings(vm, $('#dropzone')[0]);

I know the options object isn't the problem because this works perfectly

var knockoutValidationSettings = {
    insertMessages: true,
    decorateElement: true,
    errorMessageClass: 'error',
    errorElementClass: 'error',
    errorClass: 'error',
    errorsAsTitle: true,
    parseInputAttributes: false,
    messagesOnModified: true,
    decorateElementOnModified: true,
    decorateInputElement: true
};
//ko.validation.init(knockoutValidationSettings);

ko.applyBindingsWithValidation(vm, $('#dropzone')[0], knockoutValidationSettings);

How can I get the init function to work? Obviously I'd like to move this initialization to a single place at the root of my application.

like image 559
Adam Rackis Avatar asked Dec 08 '22 09:12

Adam Rackis


1 Answers

It seems that the validation plugin has been already initialized by the time you call ko.validation.init.

So you need to pass in true as the second argument to force the initialization to use the new configuration:

ko.validation.init(knockoutValidationSettings, true);

What can initialize the validation plugin?

  • any call to ko.applyBindings or ko.applyBindingsWithValidation
  • any call to ko.validation.init or ko.validation.configure

The ko.applyBindingsWithValidation version works because it updates the configuration - under the covers - even if the plugin has been already initialized.

like image 131
nemesv Avatar answered Dec 22 '22 15:12

nemesv