Monday, June 22, 2015

Difference between apex:message,apex:messages,apex:pageMessage and apex:pageMessages in salesforce

We have different components that supports displaying messages in a visual force page like <apex:message>,<apex:messages>,<apex:pageMessage> and <apex:pageMessages>. Let us see how each component behaves.

Lets take some sample code to understand each component. We have not included any message components in the VF.

<apex:page controller="MyRegisterCon2" sidebar="false" docType="HTML-5.0"   >

<apex:form >
  <apex:pageBlock >
  <apex:pageblockSection columns="1" >
  <apex:inputtext value="{!FirstName}" label="First Name"  />
  <apex:inputtext value="{!LastName}" label="Last Name" />
  <apex:input value="{!Jdate}" label="DOB" type="date" />
  <apex:inputtext value="{!Phone}" label="Phone"  id="phone"/>
  </apex:pageblockSection>
  <apex:pageBlockButtons >
  <apex:commandButton value="submit" action="{!save}" />
  </apex:pageBlockButtons>
  </apex:pageBlock>
  </apex:form>
</apex:page>

public with sharing class MyRegisterCon2 {

    public Date Jdate { get; set; }
   
    public Integer Phone { get; set; }
    
    public String LastName { get; set; }

    public String FirstName { get; set; }
    
    public PageReference save() {
    
        return null;
    }
       

}





Though phone and DOB are not strings it is not showing any errors when you click on submit.


<apex:message>:

This  component is used to display warning or error message for a specific component.

Change visualforce to following code.

<apex:page controller="MyRegisterCon2" sidebar="false" docType="HTML-5.0"   >

<apex:form >
  <apex:pageBlock >
  <apex:pageblockSection columns="1" >
  <apex:message for="phone" />
  <apex:inputtext value="{!FirstName}" label="First Name"  />
  <apex:inputtext value="{!LastName}" label="Last Name" />
  <apex:input value="{!Jdate}" label="DOB" type="date" id="dob" />
  <apex:inputtext value="{!Phone}" label="Phone"  id="phone"  />

  </apex:pageblockSection>
  <apex:pageBlockButtons >
  <apex:commandButton value="submit" action="{!save}" />
  </apex:pageBlockButtons>
  </apex:pageBlock>
  </apex:form>

</apex:page>







In above code I have included  <apex:message for="phone" /> . Though we have given text for DOB and Phone fields the message is shown for only specific component.
 i.e. Phone.

<apex:messages>

Include above code in VF page it will display all the messages . If we observe styling was not applied to the messages.

<apex:page controller="MyRegisterCon2" sidebar="false" docType="HTML-5.0"   >

<apex:form >
  <apex:pageBlock >
  <apex:pageblockSection columns="1" >
  <apex:messages />
  <apex:inputtext value="{!FirstName}" label="First Name"  />
  <apex:inputtext value="{!LastName}" label="Last Name" />
  <apex:input value="{!Jdate}" label="DOB" type="date" id="dob" />
  <apex:inputtext value="{!Phone}" label="Phone"  id="phone"  />
  </apex:pageblockSection>
  <apex:pageBlockButtons >
  <apex:commandButton value="submit" action="{!save}" />
  </apex:pageBlockButtons>
  </apex:pageBlock>
  </apex:form>

</apex:page>





<apex:pageMessages>

The  above component provides the SF styling for the messages. 

<apex:page controller="MyRegisterCon2" sidebar="false" docType="HTML-5.0"   >

<apex:form >
  <apex:pageBlock >
  <apex:pageblockSection columns="1" >
  <apex:pageMessages />
  <apex:inputtext value="{!FirstName}" label="First Name"  />
  <apex:inputtext value="{!LastName}" label="Last Name" />
  <apex:input value="{!Jdate}" label="DOB" type="date" id="dob" />
  <apex:inputtext value="{!Phone}" label="Phone"  id="phone"  />
  </apex:pageblockSection>
  <apex:pageBlockButtons >
  <apex:commandButton value="submit" action="{!save}" />
  </apex:pageBlockButtons>
  </apex:pageBlock>
  </apex:form>

</apex:page>





<apex:pageMessage>

This component is used to dispaly custom messages with severity error,warning etc.  in the VF page.

<apex:page controller="MyRegisterCon2" sidebar="false" docType="HTML-5.0"   >

<apex:form >
  <apex:pageBlock >
  <apex:pageblockSection columns="1" >
  <apex:pageMessage summary="This is page message"  severity="warning" strength="3" />
  <apex:inputtext value="{!FirstName}" label="First Name"  />
  <apex:inputtext value="{!LastName}" label="Last Name" />
  <apex:input value="{!Jdate}" label="DOB" type="date" id="dob" />
  <apex:inputtext value="{!Phone}" label="Phone"  id="phone"  />
  </apex:pageblockSection>
  <apex:pageBlockButtons >
  <apex:commandButton value="submit" action="{!save}" />
  </apex:pageBlockButtons>
  </apex:pageBlock>
  </apex:form>

</apex:page>




Hope this will be helpful to understand differrent messages in VF.

Thanks,
Naveen.

3 comments:

  1. Excellent Explanation. Can you please also explain how the controller code can add messages in VF page in each of above 4 contexts?

    ReplyDelete