Unfortunately, Ralph's post is not 100% correct (see Prady's comment that it won't work for top-of-page validation errors), so I write this answer to clearify.
- Add
<apex:pageMessages/> tag to your page
- Enclose your DML statement with try catch like this:
try{
update account; //or anything else
} catch(System.DmlException e) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getDmlMessage(0)));
}
This will print the first DML error that statement caused which is usually what you want.
You can also try e.getMessage() but this will show additional information (like Update failed. First exception on row 0 with id 001L000000QgmomIAB; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION etc.) which is not user friendly. The advantage of e.getDmlMessage is that it prints only the validation rule when the error is caused by validation rule.