View Full Version : Form isDirty after loading record

7 Oct 2013, 10:58 AM
I am loading a record into a form like so:

currentSourcerecord = sourceStore.getAt(cursor);

But now sourceForm.isDirty() = true even though the user has not made any changes to the form fields.

I tried this per a suggestion on the forum:

currentSourcerecord = sourceStore.getAt(cursor);

But .reset() is clearing the record from the form even though the docs say this should not be happening.

What is the best way to check if the form has been dirtied by the user changing a value on the form field? I can imagine a few ugly ways to do this, but would rather not do that.

This forum post: http://www.sencha.com/forum/showthread.php?185801 seems to show the same issue and has not been resolved.

I am using 4.2.


7 Oct 2013, 9:39 PM
Configure your form with trackResetOnLoad (http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.form.Panel-cfg-trackResetOnLoad).

8 Oct 2013, 6:39 AM
From getrecord method of formPanel , you can get model and the getChanges method on model gives you list of changes that took place. If list is empty then the changes are not done. This way you can figure out if form is dirty or not.

9 Oct 2013, 10:29 AM

That worked to fix the record clearing and the isDirty() is now reset.

One problem though, any fields that you manually set get cleared if you do it wrong.

I have an item in the dockedItems section with a textfield xtype which I use for record navigation, e.g. Record 4 of 421. This "field" is not in the same place as the actual form fields, but I guess Ext is treating it the same. If I set the value like Ext.getCmp("sourceFieldCount").setValue('Record 4 of 421') on the field value after the Reset() , my form isDirty(), if I set it before Reset(), it get cleared. Catch-22!

Well, a full reading of the trackResetOnLoad (http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.form.Panel-cfg-trackResetOnLoad) parameter indicates I should be doing sourceForm.getForm().setValues() instead and that does indeed work! Apparently I have been setting form values the hard way for a long time.


Ary, I could not get your technique working. This is what I did:

var record = sourceForm.getForm().getRecord();
if (record) {
var changes = record.getChanges();

changes was always empty no matter what changes I made to the form.

10 Oct 2013, 1:39 AM
If you wish to handle that field completely yourself, you can specify isFromField:false for it. Form won't even now about this field, so setValues won't work.
However in this case (self-handling) I would use a component with tpl (http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.AbstractComponent-cfg-tpl) specified and update (http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.AbstractComponent-method-update) it with data anytime I want.