function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Linda 98Linda 98 

Change location of error message on VF page

How can I change the location of error message.? I have a button which when clicked if required fields are not filled then I want to display error message above the button.

Now it's displaying on top of the page. I want to change the location of the error message. Changed in CSS to display bottom. Still no luck.Anything that I am missing.???
Khan AnasKhan Anas (Salesforce Developers) 
Hi Linda,

Greetings to you!

You can display an error message to a particular field in VF page. You need to use addError on the field in question, which also implies that you must directly bind the field to an apex:inputField. If the field is not directly bound to a visible input, it will appear at the top of the page.

Visualforce:
<apex:page controller="ErrorMessageC">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection columns="1">
                <apex:inputField value="{!record.Name}" />
            </apex:pageBlockSection>
            <apex:pageBlockButtons>
                <apex:commandButton action="{!save}" value="Save" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class ErrorMessageC {
    
    public Account record { get; set; }
    
    public ErrorMessageC() {
        record = new Account();
    }

    public void save() {
        if(record.Name == null) {
            record.Name.addError('You must enter a value!');
        }
    }
}

Please refer to the below links which might help you further with the above requirement.

https://salesforce.stackexchange.com/questions/60141/how-to-display-different-error-messages-at-different-places-on-a-visualforce-pag

https://www.forcetalks.com/blog/adding-error-messages-to-field-inputs/

https://developer.salesforce.com/forums/?id=906F000000096mcIAA

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas