[SalesForce] How to use apex:actionRegion with required fields and apex:pageBlockSection

I have a custom object with a lookup field to Opportunity. When the opportunity lookup field is changed, I have apex code that gets some data and populates some fields on a portion of the page. So, I'm using apex:actionSupport to call apex when the lookup changes and rerender the block.

The problem is, there's a required field inside the same block that is validating. I realize the solution to this is to use apex:actionRegion and I think I have a good grasp of how that works. However, I can't quite see how to get the placement of the actionRegion correct in this particular instance. Here's one variation of code I've tried:

<apex:pageBlock title="Page block" mode="edit" id="infoblock">

        <apex:pageBlockSection title="Information" columns="1">
            <apex:inputField value="{!myCustomObject.Opportunity__c}">              
                <apex:actionSupport event="onchange" action="{!changeOpportunity}" rerender="infoblock"/>               
            </apex:inputField>

            <!-- THIS IS SENT TO THE SERVER FOR VALIDATION ON OPPORTUNITY LOOKUP CHANGE -->
            <apex:inputField value="{!myCustomObject.MyRequiredField__c}" required="true" /> 

        </apex:pageBlockSection>

        <!-- DOES NOT CONTAIN VALUES ON RERENDER -->
        <apex:actionRegion> 
            <apex:pageBlockSection title="Additional Info" columns="1" id="additionalinfo">         
                <apex:inputField value="{!myCustomObject.Field1__c}" />
                <apex:inputField value="{!myCustomObject.Field3__c}" />
                <apex:inputField value="{!myCustomObject.Field3__c}" />
                <apex:inputField value="{!myCustomObject.Field4__c}" />         
            </apex:pageBlockSection>
        </apex:actionRegion>
    </apex:pageBlock>

Here's another example I tried that did not work (only rerender the additionalinfo block):

<apex:pageBlock title="Page block" mode="edit" id="infoblock">

        <apex:pageBlockSection title="Information" columns="1">
            <apex:inputField value="{!myCustomObject.Opportunity__c}">              
                <apex:actionSupport event="onchange" action="{!changeOpportunity}" rerender="additionalinfo"/>              
            </apex:inputField>

            <apex:inputField value="{!myCustomObject.MyRequiredField__c}" required="true" /> 

        </apex:pageBlockSection>

        <apex:actionRegion> 
            <apex:pageBlockSection title="Additional Info" columns="1" id="additionalinfo">         
                <apex:inputField value="{!myCustomObject.Field1__c}" />
                <apex:inputField value="{!myCustomObject.Field3__c}" />
                <apex:inputField value="{!myCustomObject.Field3__c}" />
                <apex:inputField value="{!myCustomObject.Field4__c}" />         
            </apex:pageBlockSection>
        </apex:actionRegion>
    </apex:pageBlock>

Is there a way to place the actionRegion and rerender the appropriate block so that the additionalinfo block rerenders without validating the required field? NOTE: I can only use the immediate attribute if there's a way to not lose the viewstate values.

Best Answer

actionRegion does not define the target components that gets rerendered.

It defines the source components that needs to be processed at the server end when the request is sent.

so in your case, if you just wrap the opportunity lookup inputField inside the actionRegion, it should work and skip the required field check

<apex:pageBlock title="Page block" mode="edit" id="infoblock">

        <apex:pageBlockSection title="Information" columns="1">
          <apex:actionRegion> 
               <apex:inputField value="{!myCustomObject.Opportunity__c}">              
                  <apex:actionSupport event="onchange" action="{!changeOpportunity}" rerender="additionalinfo"/>              
               </apex:inputField>
          </apex:actionRegion>    

          <apex:inputField value="{!myCustomObject.MyRequiredField__c}" required="true" /> 

        </apex:pageBlockSection>


        <apex:pageBlockSection title="Additional Info" columns="1" id="additionalinfo">         
                <apex:inputField value="{!myCustomObject.Field1__c}" />
                <apex:inputField value="{!myCustomObject.Field3__c}" />
                <apex:inputField value="{!myCustomObject.Field3__c}" />
                <apex:inputField value="{!myCustomObject.Field4__c}" />         
        </apex:pageBlockSection>

 </apex:pageBlock>