梦幻小子 发表于 2013-1-29 10:30:55

关于ext3.2.1异步验证问题

之前做了一个类似于id异步验证的功能,这涉及到表单提交前做异步验证和不点提交也需给出相应提醒的问题,用的是ext3.2.1,选这个没有为什么,因为公司正在用。咱做IT的,公司用什么,就得学什么。

以下是从网上找的2种ext异步验证方法,我觉得都不靠谱。

1、将IsExsit设置为全局变量,但我按着做不起效果,存在问题:还未验证完,数据被提交了。
            validator :function()
                   {
                   var username = Ext.get('username').dom.value;
                   Ext.Ajax.request({
                      url:'CheckUserName.aspx',
                      params:{name:username},
                      success: function(response, options) {            
                         var data = Ext.util.JSON.decode(response.responseText);            
                         if(data.success == true)ReturnValue(true);
                         else ReturnValue(false);//不能在success方法里面直接返回。
                      }
                  });
               function ReturnValue(ok){
                IsExsit = ok;
            }
            return IsExsit;
                   },

2、添加listeners配置项,listeners:{'keyup':{fn:chkOnServer}};添加配置项validationEvent:'blur',默认是“keyup”。必须注意要让监听“keyup”起效,还需要添加配置项enableKeyEvents:true,这项在api中是查不到的,而是写在keyup事件的描述中。

编写chkOnServer方法,chkOnServer(field,_event){},field为该field的引用,_event为这次事件,还没研究过是不是js原生的event,应该会有些扩展。这个方法只做ajax请求就行了,它的回调函数为changeRegBool(response,option)。
   
这么做,1中的问题是解决了,但效率太低,因为它是在文本框中每输入一个键就向服务器发起一次验证。

以下是本人的解决方案
1、监听‘blur’事件,当光标离开文本域时验证一次,起到友好的提示效果。
'blur':function(_tf){
      Ext.Ajax.request({
   url:ctx + "/modelsMainAction.do?method=rightCodeIsExist",
   params:{rightCode:_tf.getValue()},
   success: function(response, opts) {
var obj = Ext.decode(response.responseText);
var _isExist = obj["isExist"];
isExist =!_isExist;
this.validate();
},
scope:this
   });
      }
2、提交前再向后台发起一次验证,成功后再提交数据。
if(!_formPanel.getForm().isValid()) return;
var _tf = _formPanel.findByType("textfield");
Ext.Ajax.request({
   url:ctx + "/modelsMainAction.do?method=rightCodeIsExist",
   params:{rightCode:_tf.getValue()},
   success: function(response, opts) {
var obj = Ext.decode(response.responseText);
var _isExist = obj["isExist"];
isExist =!_isExist;
if(_tf.validate())
_formPanel.getForm().doAction('submit',{
url:ctx + "/modelsMainAction.do?method=modelSave",
method: 'POST',
success:function(){
var _isLeaf = true;
if(this.form.findByType("combo").getValue() == "否") _isLeaf = false;
var _node = new Ext.tree.AsyncTreeNode({
            text:this.form.findByType("textfield").getValue(),
            uiProvider:Ext.tree.ColumnNodeUI,
            leaf:_isLeaf,
            id:this.form.findByType("textfield").getValue()
            });
_node.attributes.rightFullName =this.form.findByType("textfield").getValue();
_node.attributes.rightCode =this.form.findByType("textfield").getValue();
_node.attributes.href =this.form.findByType("textfield").getValue();
_node.attributes.hrefTarget =this.form.findByType("textfield").getValue();
_node.attributes.upRightCode =this.form.findByType("textfield").getValue();
_node.attributes.rightType =this.form.findByType("combo").getValue();
menuNode.appendChild(_node);
this.close();
},
waitTitle:"数据传输",
waitMsg:"数据传输中,请稍后。。。",
scope:this}
);
},
scope:this
   });
页: [1]
查看完整版本: 关于ext3.2.1异步验证问题