Mihail Zheludev
Mihail Zheludev

Reputation: 166

KnockoutJS-Validation: initialization isn't being applied

I using knockout-validation plugin. But when I initialize custom options:

viewModel{
    model:{
       id: ko.observable(''),
       title: ko.observable('').extend({required: {message: mymessage}})
    },
    testFunc: function(){
        ....
    }
}

...

ko.applyBindings(this.viewModel, document.getElementById('mainView'));

ko.validation.configure({
       errorMessageClass: 'error-message',
       errorElementClass: 'error',
       decorateInputElement: true
});

this.viewModel.model.errors = ko.validation.group(this.viewModel.model);

they aren't being applied. If validating model, and show errors - getting default plugin message without my class error-message and input isn't decorated with error class.

But they are applied when I added them to markup:

<div id="mainView">

   <table data-bind="with: model">     

      <tbody data-bind="validationOptions {errorMessageClass: 'error-message',
          errorElementClass: 'error',
          decorateInputElement: true}">

          ...  
          <td>
               <input data-bind="value: title" type="text">
          </td> 
          ...

      </tbody>

   </table>

</div>

Upvotes: 1

Views: 627

Answers (1)

nemesv
nemesv

Reputation: 139768

In order to the validation plugin provides some of its functionality and your setup applied correctly you need to configure the validation plugin before any of the bindings applied so before calling ko.applyBindings.

So you just need to change the order of those lines:

ko.validation.configure({
       errorMessageClass: 'error-message',
       errorElementClass: 'error',
       decorateInputElement: true
});

ko.applyBindings(this.viewModel, document.getElementById('mainView'));

Upvotes: 1

Related Questions