[SalesForce] PageMessages writing duplicate error message

I have a simple visualforce page like so…

<apex:page controller="MerchantDetails">
<apex:form >
    <apex:pageBlock title="Confirm or solicit the fields below">
        <apex:pageMessages />
        <apex:pageBlockSection>
            //data...
            <apex:inputField label="Date of Birth" value="{!contact.Birthdate}" required="true"/><br />
            //more data...
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>

For the input fields where the required and label attributes are set, the pageMessages will write out duplicate messages. For example…

Errors

Date of Birth: Invalid Date

Birthdate: Invalid Date

Notice that pageMessages thinks it needs to say it is an invalid date for the label attribute and the value attribute. I only want one of the messages to be displayed. Why would salesforce write both messages? Is there something I am doing wrong? What is the best way to handle this?

Best Answer

One way to handle this would be to use a separate apex:outputLabel. E.g.

<apex:pageBlockSection>
    //data...
    <apex:pageBlockSectionItem>
        <apex:outputLabel value="Date of Birth" for="dob" />
        <apex:inputField id="dob" value="{!contact.Birthdate}" required="true"/>
    </apex:pageBlockSectionItem>
    //more data...
</apex:pageBlockSection>

This doesn't really address why you are getting double validation page messages.

One thing I did find. If you change the label attribute to match the field label of the field you only get one validation message. By this stage you could just exclude the label attribute entirely and use Christopher's suggestion to customise the label everywhere.

<apex:page StandardController="Contact">
    <apex:form >
        <apex:pageBlock title="Confirm or solicit the fields below">
            <apex:pageMessages />
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection>
                <apex:inputField label="Birthdate" value="{!contact.Birthdate}" required="true"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Single error message with matching label

Related Topic