Results 1 to 6 of 6

Thread: RadioField and isDirty problem

  1. #1
    Sencha User alexmufatti's Avatar
    Join Date
    Dec 2011
    Location
    Italy
    Posts
    13
    Vote Rating
    1
      0  

    Exclamation Unanswered: RadioField and isDirty problem

    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?

  2. #2
    Sencha - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,412
    Answers
    3898
    Vote Rating
    1287
      0  

    Default

    What version are you using?
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it!

    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User alexmufatti's Avatar
    Join Date
    Dec 2011
    Location
    Italy
    Posts
    13
    Vote Rating
    1
      0  

    Default

    I'm using 4.0.7 sandboxed

  4. #4
    Sencha User alexmufatti's Avatar
    Join Date
    Dec 2011
    Location
    Italy
    Posts
    13
    Vote Rating
    1
      0  

    Default

    No one with some information about this issue?


  5. #5
    Sencha Premium Member
    Join Date
    Oct 2009
    Location
    Milan
    Posts
    12
    Vote Rating
    0
      0  

    Default

    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

    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);
        }
    });

  6. #6
    Sencha User
    Join Date
    Aug 2007
    Posts
    7
    Vote Rating
    23
      0  

    Default

    Alex, TL;DR, here is the bug fix for 4.0.7:

    Code:
    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:

    Code:
    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:

    Code:
    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:

    Code:
    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •