0

I have a VF page to make a record for a custom object. It has 2 <pageBlockSection>. The first should always be rendered, and it contains a picklist. Depending on the value of the picklist, the second might be rendered.

What is the best way to go about this? Can the standard controller handle this, or does it need a custom controller? Why isn't my second section rendering correctly?

The picklist:

<apex:inputField value="{!CustomObject__c.Type__c}"> 
     <apex:actionSupport event="onchange" reRender="section2"/>
</apex:inputField>

And the second section:

<apex:pageBlockSection id="section2" columns="1" rendered="{!IF(CustomObject__c.Type__c == 'Functionality Request',true,false)}">  
    <apex:inputField value="{!CustomObject__c.Priority__c }"/>

</apex:pageBlockSection>
  • Can you tell if field dependency is already setup in that Object for these fields Type__c & Priority__c ? If yes, standard controller can handle this. – night crawler Jan 20 '17 at 21:36
  • 1
    rendered="{!(CustomObject__c.Type__c == 'Functionality Request')}" – o-lexi Jan 20 '17 at 21:53
  • I just used Priority as an example. The end result pageBlockSection will have like 5 or 6 fields of all different types in it. – denvergreene Jan 20 '17 at 21:55
  • Think the point is that = is an assignment whereas == is a comparison. – Keith C Jan 20 '17 at 21:56
  • Thanks all. The == is probably an error, but when I put that in, it still doesn't work as expected. – denvergreene Jan 20 '17 at 21:58
3

A custom controller is not needed. You need to re-render the container holding the component that has the conditional rendering expression.

Here is a working example:

<apex:page standardController="Contact">
<apex:form>
<apex:pageBlock id="pb">
    <apex:pageBlockSection>  
        <apex:inputField value="{!Contact.Salutation}"> 
             <apex:actionSupport event="onchange" reRender="pb"/>
        </apex:inputField>
    </apex:pageBlockSection>
    <apex:pageBlockSection id="pbs" rendered="{! Contact.Salutation == 'Mr.' }">  
        <apex:inputField value="{!Contact.Birthdate }"/>
    </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

where the Birthdate appears if "Mr." is selected. If you change it to reRender="pbs" it no longer works.

  • Perfect, this works. Thank you. I'm trying to understand why the pageBlockSection doesn't count as a container that can be rerendered and why it needs to be the pageBlock. Any good resources suggested for reading about these nuances? – denvergreene Jan 21 '17 at 0:52
  • @denvergreene There may be but I don't know of any; re-rendering is one of those "always expect trouble" features. My understanding (that may be wrong is that you always have to re-render the container of the item so its the structure more than the specific tags. – Keith C Jan 21 '17 at 10:38

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.