4

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?

2
  • 2
    Looks like a platform bug to me. Did you check for known issues?
    – Matt Lacey
    Jun 10 '14 at 22:11
  • I just did a search and did not find anything. Maybe I will submit the issue...
    – Mr. Ant
    Jun 11 '14 at 13:59
5

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

1
  • 2
    The above solution works, I noticed it will always display the internal label for the field in the error message, ie "Birthdate". As an additional suggestion, if you want to change this, you may want to consider changing the Contact Birthdate field label through the setup menu (Setup -> App Setup -> Customize -> Tab Names and Labels -> Rename Tabs and Labels). Jun 10 '14 at 22: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.