8

In my project I have custom object called MyObject__c, it contains two field:

  • Field1__c - picklist (with 2 values 'Yes'/'No')

  • Field2__c - text

And I have this code (2 classes and one VF-page):

MyWrapper class

public class MyWrapper {

    public MyObject__c myObject {get;set;}

    public void addObject(MyObject__c newObject) {
        myObject = newObject;
    }
}

MyController class

public  class MyController {

    public MyWrapper wrapper {get;set;}

    public MyController() {
        wrapper = new MyWrapper();
        addMyObject();
    }

    public void addMyObject() {
        wrapper.addObject(new MyObject__c());
    }

    public void refreshMyObject() {
        if (wrapper.myObject.Field1__c != 'Yes') {
            wrapper.myObject.Field2__c = null;
        }
    }
}

and MyPage

<apex:page showHeader="true" sidebar="true" controller="MyController">

    <apex:form>
        <apex:outputPanel layout="Block" id="myPanel">
            <apex:inputField value="{!wrapper.myObject.Field1__c}">
                <apex:actionSupport event="onchange" action="{!refreshMyObject}" reRender="myPanel"/>
            </apex:inputField>
            <apex:outputPanel layout="none" rendered="{!wrapper.myObject.Field1__C == 'Yes'}">
                <apex:inputField value="{!wrapper.myObject.Field2__c}"/>
            </apex:outputPanel>
        </apex:outputPanel>
    </apex:form>
</apex:page>

And on MyPage I perform next steps:

  1. Change Field1__c value to 'Yes', after this step I can see input for Field2__c
  2. Input text in Field2__c, for example 'aaaa'
  3. Change Field1__c value to 'No', after this step I can't see input for Field2__c and in myWrapper.myObject.Field2__c in viewstate I see null(!) value.
  4. Change Field1__c value to 'Yes', after this step I can see input for Field2__c with value 'aaaa'

Can you explain this behaviour.

Thanks for all, and sorry for my english.

3
  • that's an interesting issue. I was able to reproduce it in IE and firefox, so it's not browser/auto-complete. Issue also persists if you change refreshMyObject() to return PageReference. So a bit stumped, would like to know the asnswer as well Apr 10, 2015 at 16:38
  • @KirillYunussov and one more interesting detail. If I change myObject to list (MyObject__c[] myObject) and use with apex:repeat in VF I have expected behavior (Field2__c in step 4 cleared). After that I try to use apex:variable (myObject isn't array in this case) but with this I have the same result as in my question.
    – misha_m
    Apr 10, 2015 at 17:22
  • Great question.
    – martin
    Feb 22, 2016 at 12:07

1 Answer 1

1

Has to do with timing of rendering VF into HTML, and, browser memory. That value persists in browser memory so that when you re-render you still see the old value.

The fix is simple, add this line to your visualforce page so that the browser can "see" (and thus reset) Field2__c. Add this line, somewhere inside your form tag should be fine:

<apex:inputHidden value="{!wrapper.myObject.Field2__c}" id="theHiddenInput"/>

This is somewhat analogous to how a URL-hack button won't set field values if the fields are not present on the page layout.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.