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:
- Change Field1__c value to 'Yes', after this step I can see input for Field2__c
- Input text in Field2__c, for example 'aaaa'
- 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.
- 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.