knockout.js - How to tie together ko.validation errors with related viewmodel field names -


i'm using knockout.validation , i'd able display error summary each line displays error message (obviously!) , field name on view model related it, e.g.

  • age - please enter number
  • date of birth - please enter proper date

so far i've got validatedobservable wrapping view model, , puts errors array on viewmodel automatically, containing errors. can't see easy way retrieve field each error relates to.

i know traverse view model myself, building errors collection of own isvalid property - option though?

once have field name, can map validation summary related "friendly" label field (e.g. "date of birth" rather "dateofbirth").

here's simplified version of code have far:

viewmodel

function personmodel(){    var self = this;    self.age = ko.observable().extend({ number: true});     self.dateofbirth = ko.observable({ date: true});                 self.validate = function() {                                   if (!self.isvalid()) {                                                    self.errors.showallmessages();                   return false;                  }        return true;     };      ko.validation.init({                 grouping: { deep: true, observable: true },                 registerextenders: true,                 messagesonmodified: true,                 insertmessages: true             });  ko.applybindings(ko.validatedobservable(new personmodel())); 

html

<ul data-bind='foreach: model().errors' class="message">     <li>            <span data-bind='text:  $data'></span>     </li> </ul> 

many thanks

you can use custom validation message variable.

emailaddress: ko.observable().extend({     required: { message: 'email address: required field.' } }), 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -