2012-09-26 246 views
7

基于以下内容,我如何设置回调以显示自定义错误消息而不是默认消息?基因敲除验证插件自定义错误消息

ko.validation.rules['exampleAsync'] = { 
    async: true, // the flag that says "Hey I'm Async!" 
    validator: function (val, otherVal, callBack) { // yes, you get a 'callback' 

     /* some logic here */ 

     // hand my result back to the callback 
     callback(/* true or false */); 
     // or if you want to specify a specific message 
     callback(/* { isValid: true, message: "Lorem Ipsum" } */); 
    }, 
    message: 'My default invalid message' 
}; 

回答

5
ko.validation.rules['exampleAsync'] = { 
    async: true, 
    validator: function (val, otherVal, callBack) { 

     // make an ajax call or something here to do your async validation 
     $.ajax({ type: 'post', url: 'some url', data: val, success: function (data) { 
      if (data.success) { 
       callback({ isValid: true, message: "yay it worked"}); 
      } else { 
       callback({ isValid: false, message: data.message }); 
      } 
     }); 
    }, 
    message: 'My default invalid message' 
}; 
+0

时候会功能'ko.validation.rules [ 'exampleAsync']'叫 – SrinivasNaidu

+0

@SrinivasNaidu这不是一个功能。现在,在验证通过'extend'附加到的observable的更改被调用时,会调用'validator'函数。 – ZenMaster