Displaying error message from Apex Code
You can display error messages using the addError method on sobject or its fields.
Salesforce docs on the sobject instance addError method: https://developer.salesforce.com/docs/atlas.en-us.198.0.apexcode.meta/apexcode/apex_methods_system_sobject.htm#apex_System_SObject_addError
More info on exception handling: http://wiki.developerforce.com/page/An_Introduction_to_Exception_Handling
Thank you for your answer. addError works, however it appends "Error: Invalid Data. Review all error messages below to correct your data." before the error message. Any ideas how to disable that?
Fixed link in case someone else will try to click the first link in the answer above: https://developer.salesforce.com/docs/atlas.en-us.198.0.apexcode.meta/apexcode/apex_methods_system_sobject.htm#apex_System_SObject_addError