Search code examples
knockout.jsknockout-validation

ValidationMessage throwing observable is not validatable


I have a viewmodel and it has an observable, and there are some validations I applied on it.

Now when I use the custom validationMessage binding, to display error, I get the following error in ko validation library:

<script type="text/html" id="TEXTBOX_Template">
    <input type="text" data-bind="value: question().s_answer" />
    <span class="validationMessage" 
          data-bind="validationMessage: question().s_answer"></span>
</script>

KO validationMessage error

I am using latest version of both ko and ko-validation.

My setup is as following:

ko.validation.init({ decorateInputElement: false, 
                     messageTemplate: 'error_Template' });
ko.validation.makeBindingHandlerValidatable("selectedOptions");

Error template (but not required in this example as I want to use validationMessage):

<script type="text/html" id="error_Template">
    <!-- ko if: (field.isModified() && !field.isValid()) -->
    <span class="validationMessage" data-bind="text: field.error"></span>
    <!-- /ko -->
</script>

If this works with observable, then it should work with observableArray because I have a list of radio buttons using ko foreach and when there is validation, the error is displayed after each of the input radio buttons.

Any suggestions would help !

Thanks


Solution

  • There is some issue with the validationMessage binding, so we need to explicitly extend the observable to be validatable.

    Add the below line in the knockout.validation.js file under validationMessage binding handler:

    obsv.extend({ validatable: true });
    

    enter image description here

    Hopefully this will be fixed in next version, added an issue in ko.validation. Or atleast this worked for me, let me know if I am wrong? Or is there a better way?