PDA

View Full Version : BasicForm simple extension



Titou
25 Nov 2008, 2:57 AM
In my project, on a form load action, I was surprised that only one field value has been set when 2 fields has the same name.

For example, when a field is disabled I'm adding a hidden field before the disabled one because the disabled field value is not submitted. And on the load form action, only the hidden field value was set.

So I override the BasicForm to allow loading all the field.

Here is my code:



Ext.override(Ext.form.BasicForm, {

findField : function(id){
var fields = [];
if(this.items.containsKey(id))
{
fields.push(this.items.get(id));
}
this.items.each(function(f){
if(f.isFormField && (f.dataIndex == id || f.getName() == id)){
fields.push(f);
}
});
return fields || null;
},

updateRecord : function(record){
record.beginEdit();
var fs = record.fields;
fs.each(function(f){
var fields = this.findField(f.name);
if(fields){
record.set(f.name, fields[0].getValue());
}
}, this);
record.endEdit();
return this;
},

markInvalid : function(errors){
if(Ext.isArray(errors)){
for(var i = 0, len = errors.length; i < len; i++){
var fieldError = errors[i];
var fields = this.findField(fieldError.id);
if(fields){
$A(fields).each(function(f){
f.markInvalid(fieldError.msg);
})
}
}
}else{
var field, id;
for(id in errors){
if(typeof errors[id] != 'function' && (fields = this.findField(id))){
$A(fields).each(function(f){
f.markInvalid(errors[id]);
})
}
}
}
return this;
},

setValues : function(values){
if(Ext.isArray(values)){
for(var i = 0, len = values.length; i < len; i++){
var v = values[i];
var fields = this.findField(v.id);
if(fields){
$A(fields).each(function(f){
f.setValue(v.value);
if(this.trackResetOnLoad){
f.originalValue = f.getValue();
}
}.createDelegate(this))
}
}
}
else{
var field, id;
for(id in values){
if(typeof values[id] != 'function' && (fields = this.findField(id))){
$A(fields).each(function(f){
f.setValue(values[id]);
if(this.trackResetOnLoad){
f.originalValue = f.getValue();
}
}.createDelegate(this))
}
}
}
return this;
}
});
I override the findfield function to return and array of field having id, dataIndex or name corrsponding to the id parameter of the function. I also override functions using the findField function.

Maybe it could helps...