extJs中关于formPanel动态添加组件的验证问题

1.在用extJs的formPanel动态添加组件的时候,发现动态添加的组件验证没法通过,网上找了半天才将这个问题解决。
1.源代码如下:
Ext.onReady(function() {
  Ext.QuickTips.init();
      testWin = new Ext.Window({
func: "null",
title: "题库信息",
width: 500,
height: 420,
maximizable: true,
closeAction: "hide",
resizable: false,
plain: true,
modal: true,
layout: "fit",
items:{
xtype: "form",
id: "testForm",
layout: "form",
frame: true,
border: false,
autoScroll: true,
              labelWidth: 90,
width: 900,
items: [
new Ext.form.FieldSet({  
          name: 'testFieldSet',  
          autoHeight: true,  
          items: [
          {
          layout:'column',
          items:[
          {
          columnWidth : .9, // 该列有整行中所占百分比
        layout : "form",
        items : [
{  
                  xtype : "textfield", 
                  anchor: '90%',
                  name : "testtextvalid",  
                  fieldLabel: "testDyn", 
                  value:null,
                  allowBlank: false 
          }
          ]
          },
          {
          columnWidth : .1, // 该列有整行中所占百分比
            layout : "form",
            items : [{
    xtype:"button",
    anchor: '90%',
    text:"删除",
    handler:function(){
           var testForm = testWin.get("testForm");
           testForm.remove(this.ownerCt.ownerCt.ownerCt);
    }
    }]
          }
          ]
          }
          ]  
      })  
    ]
},
buttons: [
{
    text: "添加选项",
    width: 80,
    handler: function() {
var testForm = testWin.get("testForm");
var testFieldSet = new Ext.form.FieldSet({  
          name: 'testFieldSet',  
          autoHeight: true,  
          items: [
          {
          layout:'column',
          items:[
          {
          columnWidth : .9, // 该列有整行中所占百分比
        layout : "form",
        items : [
{  
                  xtype : "textfield", 
                  anchor: '90%',
                  name : "testtextvalid",  
                  fieldLabel: "testDyn", 
                  value:null,
                  allowBlank: false 
          }
          ]
          },
          {
          columnWidth : .1, // 该列有整行中所占百分比
            layout : "form",
            items : [{
    xtype:"button",
    anchor: '90%',
    text:"删除",
    handler:function(){
           var testForm = testWin.get("testForm");
           testForm.remove(this.ownerCt.ownerCt.ownerCt);
    }
    }]
          }
          ]
          }
          ]  
      });  
      testForm.add(testFieldSet); //add the fieldset to the formpanel  
      testFieldSet.doLayout();  
      testForm.doLayout();
      // 以下是关键代码
      testFieldSet.form = testForm.getForm();  
  testForm.initFields.call(testFieldSet);  
    }
},{
    text: "确定",
    width: 80,
    handler: function() {
// 进行验证
var valid = testWin.get("testForm").getForm().isValid();
alert(valid);
    }
}],
listeners: {
show: function(obj) {
// 题目信息窗口输入信息验证
    Ext.form.Field.prototype.msgTarget = "under";
    // 默认校验,必填项标红
    testWin.get("testForm").getForm().isValid();
},
hide: function(obj) {
// 表单重置
testWin.get("testForm").getForm().reset();
}
}
});
       function showWin(){
       testWin.show();
       }
       showWin();
});
//]]>
  </script>

你可能感兴趣的:(JavaScript,ExtJs)