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

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -