PDA

View Full Version : RadioField and isDirty problem



alexmufatti
22 Feb 2012, 5:15 AM
Hi all,
I've a problem with radiofiled in my form.

When I check if the form isDirty, I always get "true" because of some radiofiled.

I checked and I think originalValue is not correctly set when I load record on my form (I've trackResetOnLoad = true).

Anyone with this issue?

mitchellsimoens
22 Feb 2012, 5:57 AM
What version are you using?

alexmufatti
22 Feb 2012, 7:09 AM
I'm using 4.0.7 sandboxed

alexmufatti
27 Feb 2012, 12:18 AM
No one with some information about this issue?

:(

isolanet
29 Feb 2012, 9:43 AM
Hi Alex,
I've find out a possible solution overriding the setValue method of Radio field and forcing the save of originalValue.
Here is the code



Ext4.override(Ext4.form.field.Radio, {


setValue : function(v) {
var me = this, active;


if (Ext4.isBoolean(v)) {
if (Ext4.isArray(arguments)) {
me.getManager().getByName(me.name).each(function(cb) {
cb.setValue(Ext4.Array.contains(checked, cb.inputValue));
cb.originalValue = cb.getValue();
});
} else {
me.callParent(arguments);
me.originalValue = me.getValue();
}


return me;
} else {
active = me.getManager().getWithValue(me.name, v).getAt(0);
if (active) {
active.setValue(true);
active.originalValue = active.getValue();
}
}
return me;
}
});


Ext4.override(Ext4.form.field.Radio, {
isDirty : function() {
var me = this;
return !me.disabled && !me.isEqual(me.getValue(), me.originalValue);
}
});

vais
29 Feb 2012, 5:56 PM
Alex, TL;DR, here is the bug fix for 4.0.7:


Ext.override(Ext.form.field.Radio, {

resetOriginalValue: function () {
//Override the original method in Ext.form.field.Field:
//this.originalValue = this.getValue();
//this.checkDirty();
this.getManager().getByName(this.name).each(function (item) {
item.originalValue = item.getValue();
item.checkDirty();
});
}

});

For those interested, the way to track down this bug is to start with the source for Ext.form.Basic, setValues method. Inside the setValues method there is a helper function setVal that looks like this:


function setVal(fieldId, val) {
var field = me.findField(fieldId);
if (field) {
field.setValue(val);
if (me.trackResetOnLoad) {
field.resetOriginalValue();
}
}
}

You can see it calls setValue on the first field that it finds by given fieldId (field name), then resetOriginalValue. Let's take a look at setValue in Ext.form.field.Radio:


setValue: function(v) {
var me = this,
active;

if (Ext.isBoolean(v)) {
me.callParent(arguments);
} else {
active = me.getManager().getWithValue(me.name, v).getAt(0);
if (active) {
active.setValue(true);
}
}
return me;
}

This specialized setValue method takes into account that there are multiple radios with the same name, and that setValue is called on just one of them (the first one found by calling findField in basic form's setValues. So, setValue for radio field has been made "smart". Now, what about resetOriginalValue for the radio field? A specialized resetOriginalValue for radio fields was never implemented - it is simply inherited from the default in Ext.form.field.Field, which looks like this:


resetOriginalValue: function() {
this.originalValue = this.getValue();
this.checkDirty();
}

The fix then is to make resetOriginalValue as "smart" as setValue, i.e. take into account that there are multiple radio fields in the same group, with the same name.