3

I have a page where I need to display an error message for a given field, and I'm using the <apex:message> tag to display it. This all works fine if the message is rendered on first page load, but the message seems to be lost if the section is rerendered? Has anyone managed to make this work?

Sample VF Fiddle

Here's the core bit of the VF page:

<!-- sample field that controls rerendering -->
<apex:actionRegion>
  <apex:inputField value="{!Account.Industry}">
    <apex:actionSupport event="onchange" status="status" rerender="sampleBlock" />
  </apex:inputField>
</apex:actionRegion>

<!-- rendered on page load, we can see the error isn't lost -->
<apex:pageBlock title="Rendered always">
  <apex:inputField value="{!Account.Name}" id="name_always" />
  <apex:message for="name_always" />
</apex:pageBlock>

<apex:pageBlock title="Sometimes rendered" id="sampleBlock">
  <apex:variable var="ShouldRender" value="{!NOT(ISBLANK(Account.Industry))}" />

  <!-- rendered when an industry is chosen, no error message? -->
  <apex:outputPanel rendered="{!ShouldRender}">
    <apex:inputField value="{!Account.Name}" id="name" />
    <apex:message for="name" />
  </apex:outputPanel>
</apex:pageBlock>

Screen grab of the resulting page and highlighted missing error: enter image description here

0

The error that you added in the extension constructor to the Account no longer exists after the onchange event and subsequent AJAX refresh.

When the content is refreshed from the Salesforce server the constructor doesn't get called again. Notice that the red boarder around the inputField has also been cleared.

You could add an explicit action and method binding to the apex:actionSupport to call a method that will add the error again.

  • Thanks! Not sure why I didn't think of that... Here's the updated VFFiddle – nadrees Nov 5 '14 at 20:58
  • 1
    Is it the case that the standard controller does not include its record in the view state so the error is lost? Would the extension controller holding a non-transient reference to the Account cause the message to be preserved in the view state? – Keith C Nov 5 '14 at 21:00
  • It certainly appears the error added in the constructor is lost from the view state. I just tested using addError against the Name field and keeping a non-transient member reference to the controller record. They Error was gone after the round trip to the server. – Daniel Ballinger Nov 6 '14 at 0:39

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.