html5 - Knockoutjs validation and server validation -
hi i'm kind of new knockoutjs, in scenario want post form have example email address, there requirement email address needs unique.
on server check if email address unique or not , returns validationjson class example
{ isemailunique: false, ispasswordstrongenough: true; }
how can knockoutjs validation show these errors in neat way?
i use 2 different server side validators this, since affect different observables in view model.
originally taken knockout validation readme
ko.validation.rules['isemailunique'] = {    validator: function(val, param){       var isvalid = true;        $.ajax({           async: false,           url: '/validation/isemailunique',           type: 'post',           data: { value: val, param: param },           success: function(response){                  isvalid = response === true;                         },           error: function(){                  isvalid = false; //however handle                         }        });         return isvalid;   },   message: 'the email not unique' };               then on server need create endpoint accepts post requests perform lookup , return true or false depending on result of query.
to use above validator
this.email = ko.observable()    .extend({        isemailunique: {           message: 'something else perhaps? override message in validator'        }     }); you can use same thing password strength validation.
using validators fire validation when observable changes, can useful way validation.
Comments
Post a Comment