论坛首页 Web前端技术论坛

请教如何将badqiu的validator扩展dwr支持

浏览 2296 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2006-12-14  
当前该validator已经非常好用,而且已经扩展了一般的ajax支持。但是因为对prototype和这个validator不熟悉,不知道这样的功能能否实现?

比如一个id输入域,用户输入了重复的id goodboy,那么返回的信息中,可能返回类似于:“建议您使用goodboy2008”之类的信息。也就是说,需要将服务器端返回的信息呈现出来。

另外,因为dwr用的比较多,非常希望扩展dwr支持。但在下能力有限,看了半天也不知道该如何动手。

只能就我佛山人的validator做一个简单的dwr包装,用处不大,仅供参考。哪位大侠有时间了能继续badqiu版本扩展一下造福一方啊。。。。

js代码

js 代码
 
  1. var dwr = new Object();  
  2.   
  3. dwr.Validator = function(){};  
  4.   
  5. dwr.Validator.Conts = {  
  6.     waitingMsg: "正在检测数据是否可用……",  
  7.     waitingMsgClassName : "dwr_waiting",  
  8.     waitingMsgColor: "gray",  
  9. };  
  10.   
  11. dwr.Validator.prototype = {  
  12.     validateOne:function(element,remoteFunc,remoteObj,callback,requestMsg){  
  13.         if ( Validator.ValidateOne(element,3)) {  
  14.             // 如果客户端验证正确,才验证服务器端  
  15.             if ( remoteFunc != null && ( remoteFunc instanceof Function ) ) {  
  16.                     if ( null == requestMsg){  
  17.                         requestMsg = new Object();  
  18.                     }  
  19.               
  20.                     if ( typeof(requestMsg.fieldID) == "undefined") {  
  21.                         requestMsg.fieldID = element.id;  
  22.                     }  
  23.                       
  24.                     if ( typeof(requestMsg.formID) == "undefined") {  
  25.                         requestMsg.formID = element.form.id;  
  26.                     }  
  27.                       
  28.                     if ( typeof(requestMsg.fieldValue) == "undefined") {  
  29.                         requestMsg.fieldValue = $F(element);  
  30.                     }  
  31.                       
  32.                     if ( typeof(requestMsg.fieldTitle)== "undefined"){  
  33.                         requestMsg.fieldTitle = field.getAttribute("title");  
  34.                     }  
  35.                       
  36.                     var box = Validator.GetNoticeBox(element);  
  37.                       
  38.                     if ( typeof(requestMsg.msgNodeID) == "undefined") {  
  39.                         requestMsg.msgNodeID = box.id;  
  40.                     }  
  41.                     // 正在等待回调  
  42.                     box.innerHTML = dwr.Validator.Conts.waitingMsg;  
  43.                     box.className = dwr.Validator.Conts.waitingMsgClassName;  
  44.                     box.style.color = dwr.Validator.Conts.waitingMsgColor;  
  45.                       
  46.                     if ( null == callback){  
  47.                         callback = this.onDWRCallback;  
  48.                     }  
  49.                     // 调用dwr服务  
  50.                     remoteFunc.call(remoteObj,requestMsg,callback);  
  51.             }  
  52.         }  
  53.     },  
  54.   
  55.     onDWRCallback:function(responseMsg){  
  56.         // msg的类型必须严格要求  
  57.         var field = $(responseMsg.fieldID);  
  58.   
  59.         var box = Validator.GetNoticeBox(field);  
  60.           
  61.         if ( responseMsg.beAccepted )  
  62.         {  
  63.             box.innerHTML = field.getAttribute("okmsg");  
  64.             box.style.color = "green";            
  65.         } else   
  66.         {  
  67.             box.innerHTML = responseMsg.msgContent;  
  68.             box.className = responseMsg.msgClassName;  
  69.             box.style.color = responseMsg.msgColor;  
  70.         }  
  71.       
  72.         return;  
  73.   
  74.     }  
  75.   
  76. }  
  77.   
  78. var validateField = function(field)  
  79. {  
  80.     var ac = new dwr.Validator();  
  81.     ac.validateOne(field,DWRService.validate,DWRService,null,null);  
  82. }  

在html代码里面大致为:

xml 代码
 
  1. <input type="text" id="account" onBlur="validateField(this)" title="account" dataType="Username" msg="ID不能少于6个字符,只能由英文字母、数字和下划线组成" okmsg="√"  onchange="this.value = this.value.toLowerCase()"/>  


服务器端需要用到的java代码:

java 代码
 
  1. public class VRequestMessage implements Serializable {  
  2.   
  3.     /** 
  4.      *  
  5.      */  
  6.     private static final long serialVersionUID = 6203010530134833424L;  
  7.   
  8.     /** 
  9.      * 输入域节点id. 
  10.      */  
  11.     private String fieldID;  
  12.   
  13.     /** 
  14.      * 输入域名称. 
  15.      */  
  16.   
  17.     private String fieldTitle;  
  18.   
  19.     /** 
  20.      * 写入消息的节点id. 
  21.      */  
  22.     private String msgNodeID;  
  23.   
  24.     /** 
  25.      * field所在form的id. 
  26.      */  
  27.     private String formID;  
  28.   
  29.     /** 
  30.      * 输入域的值. 
  31.      */  
  32.     private String fieldValue;  
  33.   
  34. }  
  35.   
  36.   
  37.   
  38. public class VResponseMessage implements Serializable {  
  39.   
  40.     /** 
  41.      *  
  42.      */  
  43.     private static final long serialVersionUID = -3052764042556487608L;  
  44.       
  45.     /** 
  46.      * 该数据是否通过验证. 
  47.      */  
  48.     private boolean beAccepted;  
  49.   
  50.     /** 
  51.      * 返回消息的内容.可以为空. 
  52.      */  
  53.     private String msgContent;  
  54.   
  55.     /** 
  56.      * 被验证的输入域的css class名称.可以为空. 
  57.      */  
  58.     private String fieldClassName;  
  59.   
  60.     /** 
  61.      * 被验证的输入域id 
  62.      */  
  63.     private String fieldID;  
  64.   
  65.     /** 
  66.      * 返回消息的class名称.可以为空. 
  67.      */  
  68.     private String msgClassName;  
  69.   
  70.     /** 
  71.      * 返回消息的颜色.可以为空. 
  72.      */  
  73.     private String msgColor;  
  74.   
  75.     /** 
  76.      * 返回消息应该写入的节点id. 
  77.      */  
  78.     private String msgNodeID;  
  79.   
  80. }  
  81.   
  82.   
  83.   
  84. public interface IDWRValidationService {  
  85.   
  86.     public VResponseMessage validate(VRequestMessage req);  
  87. }  



   发表时间:2006-12-15  
你说的显示特定错误信息的问题可以通过在input中指定"validateFailedMessage",现在的ajax验证可以满足包括如:用户名被注册,邮件已经被注册,验证码错误等情况

你需要显示服务器返回来的特定信息问题,可以自己修改validate-ajax,我以后也会加入这方面的支持

DWR的支持可能以后会加入

最新版本可以通过这里下载: http://cvs.cosoft.org.cn/cgi-bin/viewcvs.cgi/wonder/modules/javascript/validation/
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics