Adding error messages to field inputs

When users are editing or creating a record via a Visualforce page, they will often make mistakes or enter invalid data. The required fields will present an error message underneath the field itself, but validation rules or exceptions will simply send the user to a new page with a large error message, telling them that the insert or update failed.

In this recipe, we will create a Visualforce page to allow a user to create or edit a contact record. The contact standard controller and a controller extension manage the page. The extension controller checks whether the e-mail address or phone number field has been populated. If either of the fields is populated, the record will be saved, but if both are missing, ...

Get Visualforce Development Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.