PDA

View Full Version : Ever wanted to listen for a 'change' even on a form?



johughes
3 Jun 2008, 11:10 AM
Here's a quick override I whipped up that adds a 'change' event to the BasicForm that gets fired whenever any of the form fields gets changed:


Ext.override(Ext.form.BasicForm, {
onFieldChange: function(field, newValue, oldValue) {
this.fireEvent('change', this, field, newValue, oldValue);
},
add: function(){
this.items.addAll(Array.prototype.slice.call(arguments, 0));

for(var i=0; i<arguments.length; i++)
arguments[i].on('change', this.onFieldChange, this);

return this;
},
initEl: function(el) {
this.addEvents('change');

// Unchanged code follows:
this.el = Ext.get(el);
this.id = this.el.id || Ext.id();
if(!this.standardSubmit){
this.el.on('submit', this.onSubmit, this);
}
this.el.addClass('x-form');
}
});
Suggested usage:

my_form_panel.getForm().addListener('change', function(field, newValue, oldValue) {
my_submit_button.setEnabled(true);
});
Hope this helps somebody out.

Pagebaker
3 Jun 2008, 11:31 AM
Thank you very much, this can come out handy :)