The Adding and Customizing Input Field Labels documentation says:

When set, the label attribute will be used for component-level error messages, for example, when a field is required or must be unique. Custom labels won't be used in custom error messages, and the default object field label will be used instead. If you set a label attribute to an empty string, the default object field label will be used in all error messages.

I'm not able to understand above statement; we can access custom labels in the apex with using $Label directly.


It simply means:

  1. When you have a label attribute, the component level error message would show error in context label passed in attribute.
  2. When your have a empty label attribute or does not have a label attribute at all, it would not show any message with the default field's label.

Difficult to understand; maybe it would be clear when you try the example.

Output on click of save when email is empty (code at the end)-

enter image description here

Note: Error message for second and third sections/input are same (as apex:pageMessage doesn’t show duplicate message), default message with field's label; only difference is in #3 we have empty label hence it doesn't show a field label because its expecting us to add.

Code for illustration:

<apex:page standardController="Contact">
    <apex:form id="form">
        <apex:pageBlock title="Quick Edit: {!Contact.Name}">
            <apex:pageBlockSection title="1. Dynamic Label" columns="1">
                <apex:inputText value="{!Contact.Email}" 
                    label="{!Contact.FirstName + '’s Email'}" required="true"/>
            <apex:pageBlockSection title="2. No label" columns="1">
                <apex:inputText value="{!Contact.Email}" required="true"/>
            <apex:pageBlockSection title="3. Empty label" columns="1">
                <apex:inputText value="{!Contact.Email}" label="" required="true"/>
            <apex:pageBlockSection title="4. From custom Label" columns="1">
                <apex:inputText value="{!Contact.Email}" 
                    label="{!$Label.Label_Contact_Email}" required="true"/>
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save" rerender="form"/>
  • Very well explained Rahul. I am a bit confused about the error not appearing in 3rd section and the statement If you set a label attribute to an empty string, the default object field label will be used in all error messages. Both seems contradictory Mar 31 '18 at 14:59
  • 1
    apex:pageMessage's does not show duplicate messages, its the same for #2 and 3 as both use the default field's label from metadata.
    – Raul
    Mar 31 '18 at 15:02

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.