anya.org
aby 开发软件
posts - 0,  comments - 0,  trackbacks - 0
Ext中的表单提示, 有时候不是那么的尽人意,  于是我们就会想到重写它的方法,  于是笔者写我几个例子试试,
 1Ext.form.FormPanel.prototype.submit = function(){
 2    if(!this.form.isValid(true))
 3        this.validateItems();
 4    else
 5        return this.form.submit.apply(this.form,arguments) 
 6}

 7
 8//## 扩展form验证
 9Ext.form.FormPanel.prototype.validateItems = function()
10{
11    try
12    {
13        var items = this.items.items;
14        var blanks = "";
15        var errorFields = [];
16        
17        for(var i = 0; i < items.length; i ++ )
18        {
19            if! items[i].isValid(true))
20            {
21                if(items[i].blankText.indexOf("This is"== -1)
22                    items[i].blankText = items[i].fieldLabel+"不能为空!";
23                blanks += items[i].blankText + "<br/>";
24                errorFields.push(items[i]);
25            }

26        }

27        blanks = blanks.substr(0, blanks.length - 5);
28        Ext.Msg.show(
29        {
30            title : '验证信息',
31            icon : Ext.MessageBox.ERROR,
32            buttons : Ext.MessageBox.OK,
33            width : 350,
34            msg : blanks.toString(),
35            fn : function()
36            {
37                for(var i = 0; i < errorFields.length; i ++ )
38                {
39                    errorFields[i].reset();
40                }

41                errorFields[0].selectText();
42                errorFields = null;
43                return;
44            }

45        }
);
46    }

47    finally
48    {
49        blanks = null;        
50        items = null;
51    }

52}
;
53Ext.form.BasicForm.prototype.validateItems = Ext.form.FormPanel.prototype.validateItems;
54
这是一个表单验证的例子, 再放一个添加vtype类型的代码
Ext.apply(Ext.form.VTypes,{

    password: 
function(val,field) {
        
if(field.passField) {
            
var pwd=Ext.getCmp(field.passField);
            
return (val==pwd.getValue());
        }

        
return true;
    }
,

    passwordText: '两次输入的密码不一致
!'
}
);
上面这个就是验证两次密码的输入是否一致,
有时候看到FormPanel里的allowBlank提示是很不爽,  它只会提示 “该输入项为必输项”  这样的提示总觉得不舒服,  于是笔者给他改成了这样
“用户名不能为空!”  等等, 注明到底是哪个为必填项,  重写Ext的 方法代码如下
Ext.form.TextField.prototype.validateValue = function(value){
     
        
if(Ext.isFunction(this.validator)) {
            
var msg=this.validator(value);
            
if(msg!==true{
                
this.markInvalid(msg);
                
return false;
            }

        }

        
if(value.length<1||value===this.emptyText) {
            
if(this.allowBlank) {
                
this.clearInvalid();
                
return true;
            }
 else {
                
if(this.blankText.length == 5)
                    
this.markInvalid(this.fieldLabel + this.blankText);
                
else
                    
this.markInvalid(this.blankText);
                
return false;
            }

        }

        
if(value.length<this.minLength) {
            
this.markInvalid(String.format(this.minLengthText,this.minLength));
            
return false;
        }

        
if(value.length>this.maxLength) {
            
this.markInvalid(String.format(this.maxLengthText,this.maxLength));
            
return false;
        }

        
if(this.vtype) {
            
var vt=Ext.form.VTypes;
            
if(!vt[this.vtype](value,this)) {
                
this.markInvalid(this.vtypeText||vt[this.vtype+'Text']);
                
return false;
            }

        }

        
if(this.regex&&!this.regex.test(value)) {
            
this.markInvalid(this.regexText);
            
return false;
        }

        
return true;
}
它就会提示这样
 
posted on 2009-11-19 22:51 xjlong 阅读(1918) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 
我的blogjava  博客开通了

<2025年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

留言簿(1)

随笔分类

文章档案

搜索

  •  

最新评论