+ Start a Discussion
Manasa.RManasa.R 

Error Message Not getting displayed on VF Page

Hi,

 

I need to display an error message when one of the fields is null. 

 VF Page :

 

<apex:page controller="conAddmailsIds" sidebar="true" showHeader="true" >
<apex:pagemessages/>
<apex:pageBlock id="block">
<apex:pageBlockSection >
<apex:panelGroup >
<center>
<apex:selectList id="select" value="{!selectId}" size="1">
<apex:selectOptions value="{!autoRun}" />
</apex:selectList>

<apex:inputText id="searchText" value="{!searchText}"/>
<apex:commandButton value="Search" action="{!getAllEmployees}" rerender="searchResult" status="status"/>
</center>

<apex:commandButton value="Hierarchy" action="{!getEmpHierarchyData}" rerender="searchResult" status="status"/>

</apex:panelGroup>
</apex:pageBlockSection>
<apex:messages/>

<apex:actionStatus id="status" startText="Searching... please wait..."/>

--------

</apex:page>

 

Controller:

 

public PageReference getAllEmployees(){
if(searchText == '' || searchText == null){
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Add your message here');
ApexPages.addMessage(myMsg);
return null;
}

......

 

I have tried with both apex:messages and apex:pagemessages, still nothing is working. Is there any page level settings to be done?

I am not able to figure out why the error message is not getting printed. Is there anything else i need to do here?

 

Thanks in advance!

 

Regards,

Manasa R

Best Answer chosen by Admin (Salesforce Developers) 
imutsavimutsav

You are only rendering 'searchResult' how ever either you render the entire page or atleast the message. Modify your code as below. 

 

Put your message tag inside <apex:pageblock> and add <apex:form>

<apex:page controller="conAddmailsIds" sidebar="true" showHeader="true" >
<apex:form>
<apex:pageBlock id="block">
<apex:pageMessages id='msg'/>
<apex:pageBlockSection >
<apex:panelGroup >
..........
.......in your command button render the messages as well
<apex:commandButton value="Search" action="{!getAllEmployees}" rerender="searchResult,msg" status="status"/>

//close the </apex:form> in the end.

Remove <apex:messages/> You don't need it.

In your controller it looks fine but try to change the add message to

ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'System Error, 'Your error message '));

Thanks
Utsav

[Do mark this answer as solution if it works for you and give a kudos.]

All Answers

imutsavimutsav

You are only rendering 'searchResult' how ever either you render the entire page or atleast the message. Modify your code as below. 

 

Put your message tag inside <apex:pageblock> and add <apex:form>

<apex:page controller="conAddmailsIds" sidebar="true" showHeader="true" >
<apex:form>
<apex:pageBlock id="block">
<apex:pageMessages id='msg'/>
<apex:pageBlockSection >
<apex:panelGroup >
..........
.......in your command button render the messages as well
<apex:commandButton value="Search" action="{!getAllEmployees}" rerender="searchResult,msg" status="status"/>

//close the </apex:form> in the end.

Remove <apex:messages/> You don't need it.

In your controller it looks fine but try to change the add message to

ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'System Error, 'Your error message '));

Thanks
Utsav

[Do mark this answer as solution if it works for you and give a kudos.]

This was selected as the best answer
Manasa.RManasa.R

Thanks a lot, it worked :)

imutsavimutsav
Welcome
skhalidskhalid
Thanks so much! Worked for me too.