Java代码
//把ext 对象绑定在Html Form元素时的ext属性中
Ext.override(Ext.Component, {
initComponent :function(){
this.on('render', function(){
if(this.el)
Ext.getDom(this.el).ext = this;
if(this.hiddenField)//Combo
Ext.getDom(this.hiddenField).ext = this;
})
}
});
­
//获取验证信息
// 将EXT的验证属性写成JSONString格式,保护在val属性中
$getValid = function(el){
var valid = "{}";
if(Ext.getDom(el).attributes['val'])
valid = Ext.getDom(el).attributes['val'].nodeValue;
return valid;
}
//自动绑定Html中的Form元素
$blindFormField = function(){
var objArray = Ext.DomQuery.select("input[type=submit]");
Ext.each(objArray, function(obj) {
var btn = new Ext.Button({
text : obj.value,
applyTo : obj,
handler : obj.onclick,
type : obj.type
});
btn.getEl().replace(Ext.get(obj));
});
var objArray = Ext.DomQuery.select("input[type=reset]");
Ext.each(objArray, function(obj) {
var btn = new Ext.Button({
text : obj.value,
applyTo : obj,
handler : obj.onclick,
type : obj.type
});
btn.getEl().replace(Ext.get(obj));
});
var objArray = Ext.DomQuery.select("input[type=button]");
Ext.each(objArray, function(obj) {
var btn = new Ext.Button({
text : obj.value,
applyTo : obj,
handler : obj.onclick,
type : obj.type
});
btn.getEl().replace(Ext.get(obj));
});
var objArray = Ext.DomQuery.select("input[type=text]");
Ext.each(objArray, function(obj) {
if(Ext.getDom(obj).ext) return;
var txtField = new Ext.form.TextField(
Ext.apply({applyTo:obj},eval_r('('+$getValid(obj)+')'))
);
});
var objArray = Ext.DomQuery.select("input[type=password]");
Ext.each(objArray, function(obj) {
var txtField = new Ext.form.CustomTextField(
Ext.apply({applyTo:obj,inputType:'password'},eval_r('('+$getValid(obj)+')'))
)
});
var objArray = Ext.DomQuery.select("input[type=file]");
Ext.each(objArray, function(obj) {
});
var objArray = Ext.DomQuery.select("textarea");
Ext.each(objArray, function(obj) {
var txtArea = new Ext.form.TextArea(
Ext.apply({applyTo:obj},eval_r('('+$getValid(obj)+')'))
);
});
var objArray = Ext.DomQuery.select("input[type=checkbox]");
Ext.each(objArray, function(obj) {
var checkbox = new Ext.form.Checkbox(
Ext.apply({applyTo:obj},eval_r('('+$getValid(obj)+')'))
);
});
var objArray = Ext.DomQuery.select("input[type=radio]");
Ext.each(objArray, function(obj) {
var radio = new Ext.form.Radio(
Ext.apply({applyTo:obj},eval_r('('+$getValid(obj)+')'))
);
});
var objArray = Ext.DomQuery.select("select");
Ext.each(objArray, function(obj, index) {
var select = new Ext.form.ComboBox(
Ext.apply({transform:obj,triggerAction:'all', forceSelection : true, cls:obj.className},eval_r('('+$getValid(obj)+')'))
);
});
var objArray = Ext.DomQuery.select("input[type=number]");
Ext.each(objArray, function(obj) {
var dateField = new Ext.form.CustomNumberField(
Ext.apply({applyTo:obj},eval_r('('+$getValid(obj)+')'))
);
});
var objArray = Ext.DomQuery.select("input[type=date]");
Ext.each(objArray, function(obj) {
var dateField = new Ext.form.CustomDateField(
Ext.apply({applyTo:obj,format:'Y-m-d',cls:obj.className},eval_r('('+$getValid(obj)+')'))
);
});
var objArray = Ext.DomQuery.select("input[type=email]");
Ext.each(objArray, function(obj) {
var emailField = new Ext.form.TextField(
Ext.apply({applyTo:obj,vtype:'email'},eval_r('('+$getValid(obj)+')'))
);
});
var objArray = Ext.DomQuery.select("input[type=custom]");
Ext.each(objArray, function(obj) {
var m_width = $(obj).width()+7;
var houseinfoField = new Ext.form.HouseInfoArea(
Ext.apply({applyTo:obj,cls:obj.className},eval_r('('+obj.val+')'))
)
});
}
//验证输入的合法性
$validEl = function(el){
if(typeof(el) == "string") el = document.getElementByIdx_x(el);
if(el.ext)
return el.ext.validate();
return true;
}
//验证form下所有元素输入的合法性,返回true为合法,false为不合法
$valid2form = function(form){
var valid = true;
var thefrm = form;
if(typeof(form) == "string") var thefrm = document.getElementByIdx_x(form);
for (i = 0; i < thefrm.elements.length; i++){
var e = thefrm.elements[i];
if(!e.name) continue;
if(!$validEl(e)){
valid = false;
}
}
return valid;
}
Ext.onReady(function() {
setTimeout($blindFormField, 0);
})
//HTML 中的使用代码
<form id='myform' action='action.do' οnsubmit=''
<table>
<tr>
<td> 字符串:</td> <td><input type='text' id='string' name='string' val='{allowBlank:false}'/></td>
<td> 数字类型:</td> <td><input type='number' id='number' name='number' val='{allowBlank:false}'/></td>
</tr>
<tr>
<td> Email:</td> <td><input type='email' id='email' name='email' val='{allowBlank:false}'/></td>
<td> 日期类型:</td> <td><input type='date' id='date' name='date' val='{allowBlank:false}'/></td>
</tr>
<tr>
<td> Select:</td>
<td>
<select name='select'>
<option value=1>option1</option>
<option value=2>option2</option>
<option value=3>option3</option>
</select>
</td>
<td> 自定义类型:</td> <td><input type='custom' id='custom' name='custom' val='{allowBlank:false}'/></td>
</tr>
<table>
</form>