1. #1
    Sencha User alexmufatti's Avatar
    Join Date
    Dec 2011
    Posts
    5
    Vote Rating
    0
    alexmufatti is on a distinguished road

      0  

    Exclamation Unanswered: RadioField and isDirty problem

    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 - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,327
    Answers
    3540
    Vote Rating
    850
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    What version are you using?
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    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
    Posts
    5
    Vote Rating
    0
    alexmufatti is on a distinguished road

      0  

    Default


    I'm using 4.0.7 sandboxed

  4. #4
    Sencha User alexmufatti's Avatar
    Join Date
    Dec 2011
    Posts
    5
    Vote Rating
    0
    alexmufatti is on a distinguished road

      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
    isolanet is on a distinguished road

      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
    2
    vais is an unknown quantity at this point

      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.

Thread Participants: 3

Tags for this Thread