+ Start a Discussion
garybgaryb 

Action regions

I'm currently trying to wrap my head around actionRegions. From what I've read, something like the following should work:

 

 

<apex:inputField id="field1" value="{!myAccount.SomeField}" required="true" />
<apex:actionRegion>
<apex:inputField id="field2" value="{!myOpp.SomeOtherField}"/>
<apex:commandButton action="{!doSomething}" value="Button Label"/>
</apex:actionRegion>

 

 

However, on clicking the button, I get a complaint that the first input field requires a value.

 

Obviously, the above is not my real code, and there may be problems because of something else on my page - but the above should work shouldn't it? Or have I misunderstood about action regions?

 

Thanks in advance...

Best Answer chosen by Admin (Salesforce Developers) 
jwetzlerjwetzler

This really depends on what you're trying to do.

 

Try this example in your own org.

 

 

<apex:page standardController="account">
  <apex:form id="theForm">
    {!account.industry}
    <apex:inputField value="{!account.name}" required="true"/>
    <apex:actionRegion>
        <apex:inputField value="{!account.industry}"/>
        <apex:commandButton value="Click" rerender="theForm"/>
    </apex:actionRegion>
  </apex:form>
</apex:page>

 It works fine.  If you select something from the picklist and click the button, you'll see the industry get updated and no message about the first field being required.

 

 

However add action="{!save}"  to your commandButton component and then select something from the picklist again and click the button.  Now you get an error message.  That's because you can't actually save the account without specifying a name.  And furthermore, even if you type something in the name field, select something from the picklist, and hit the button you'll still get the same error message because the actionRegion defines what actually gets submitted to the server, meaning that what you type in the name field will not actually get set when you click the button, thus you'll still get an error telling you it's required.

 

Also note that in my original example, if you remove the actionRegion component (and the save action from the button) and select something from the drop down and click the button, you'll get the required error message.

 

Does that clear things up for you at all?

All Answers

jwetzlerjwetzler

This really depends on what you're trying to do.

 

Try this example in your own org.

 

 

<apex:page standardController="account">
  <apex:form id="theForm">
    {!account.industry}
    <apex:inputField value="{!account.name}" required="true"/>
    <apex:actionRegion>
        <apex:inputField value="{!account.industry}"/>
        <apex:commandButton value="Click" rerender="theForm"/>
    </apex:actionRegion>
  </apex:form>
</apex:page>

 It works fine.  If you select something from the picklist and click the button, you'll see the industry get updated and no message about the first field being required.

 

 

However add action="{!save}"  to your commandButton component and then select something from the picklist again and click the button.  Now you get an error message.  That's because you can't actually save the account without specifying a name.  And furthermore, even if you type something in the name field, select something from the picklist, and hit the button you'll still get the same error message because the actionRegion defines what actually gets submitted to the server, meaning that what you type in the name field will not actually get set when you click the button, thus you'll still get an error telling you it's required.

 

Also note that in my original example, if you remove the actionRegion component (and the save action from the button) and select something from the drop down and click the button, you'll get the required error message.

 

Does that clear things up for you at all?

This was selected as the best answer
garybgaryb

"And furthermore, even if you type something in the name field, select something from the picklist, and hit the button you'll still get the same error message because the actionRegion defines what actually gets submitted to the server, meaning that what you type in the name field will not actually get set when you click the button, thus you'll still get an error telling you it's required."

 

Hmm, I think this could be the cause. Certainly given me some avenues to explore, thanks for your response!