How can I control how error messages from apex show at different places on a page?
I`d like to show ErrorA to RegionA if ErroA occured, and ErrorB to RegionB if ErrorB occured.
Does anyone have any ideas of how to do this?

Notes: base on apex:message ( or apex:messages ) or pex:pageMessage ( apex:pageMessages )

3 Answers 3


If you're using <apex:pageMessages> or <apex:messages> they're always going to pick up all messages at each point when they're rerendered.

One thing you could do is instead use <apex:pageMessage> elements with fixed content (or content from a public controller variable) and render them based on conditions, for example:

<apex:pageMessage summary="Error A Alert!" severity="warning" strength="3" rendered="{!AExploded}" />
<!-- snip -->
<apex:pageMessage summary="Error B Alert!" severity="warning" strength="3" rendered="{!BExploded}" />

Sure. You could define a variable for each region's error message, and then use the rendered property in visualforce to show error messages only when there's an error for a given region. Take a look:

In the controller:

for(Thing c : thingList){
    if (c.notesRequired == true && String.isBlank(c.Item_Notes__c)){
        // add an error message specific to this notes field.
        c.notesFieldError = 'Notes field is required for this item.';
        c.notesFieldErrorPrompt = 'Required: ' + c.itemNotesRequirement;

In the visualforce page:

<apex:outputText id="productName" value="{!c.displayName}"/>    
<apex:outputText rendered="{!c.NotesFieldError!=''}">
    <div class="errorMsg"><strong>Error:</strong>&nbsp;{!c.NotesFieldError}</div>    
<apex:inputTextArea rows="1" value="{!c.oppLineItem.Item_Notes__c}"/>
<apex:outputText rendered="{!c.NotesFieldErrorPrompt!=''}">
    <div class="errorMsg"><strong>Error:</strong>&nbsp;{!c.NotesFieldErrorPrompt}</div>    

Screenshot showing cleaner placement for error messages

This gives you a lot of flexibility in terms of message placement, and allows you to show different error messages in different regions at the same time. (Or multiple messages in the same region, if you need to get that fancy. ;)

  • In this condition, perhaps we can use the native addError method to achieve this requirement.
    – Leisure
    Commented Dec 17, 2014 at 10:00

Maybe I didn`t describe clearly my question. And I finally want to get like this:

    <apex:pageMessages id="allErrorMsg"/>
    <apex:commandButton id="cmdSubmit" action="{!submit}" value="{!$Label.VP_Submit}" 
oncomplete="hideMask();" onclick="showMask(); "  reRender="allErrorMsg" >

    <apex:pageMessages id="popWinErrorMsg" />
    <apex:commandButton action="{!save}" value="{!$Label.Save}" onclick="showMask();"  
oncomplete="hideMask(); "  reRender="popWinErrorMsg"/>

And thank you for your answers.

  • So, given the code above, you have the ability to control where and when the messages appear: Messages, are displayed in the allErrorMsg block when the cmdSubmit button is clicked. Messages are displayed in the popWinErrorMsg block when the Save button is clicked. The problem is that the code isn't letting you display different error messages in the two locations. Is that right? If so, that's where my and LaceySnr's suggestions about storing error messages in variables comes into play. Let me know if I'm understanding you correctly and I'll explain further. Commented Dec 17, 2014 at 3:42
  • Hi Shane, Id just need the effect that given the code above, therefore I said I didnt describe my question. I`m sorry for those confused words!
    – Leisure
    Commented Dec 17, 2014 at 9:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .