Customising APEX built-in validation messages
I was working on an application and when trying to save some data, I was getting these error messages despite the fact I had no validations on my page. And what I wanted to know, how do I change these message contents?
APEX does some default checking based on entered data. A prime example of this is a date field. On a brand new application, I created a region and added a page item with the type as "Date Picker". If I enter text into the date input field that is not a date conforming to the date format, I will get the following error:
"Date Field does not match format DS."
First of all, DS? What is that? This particular application was created by uploading a spreadsheet, and I did not set that. In fact, my spreadsheet had no date data to begin with.
In saying that, a quick search in the globalization attributes, and somehow this application has the date format set to "DS". So first, let us change that - I like to use DD-MON-YYYY.
So that fixes the error message from the aspect of printing out the format mask I want to see. Now it reads as:
"Date Field does not match format DD-MON-YYYY."
But what if I want to change this message entirely? I can tell this error message is referencing 2 parameters:
- Label - Date Field
- Format Mask - DD-MON-YYYY
But I may want to show an entirely different message when the user enters bad data - what can I do?
I don't come from a background of working with translated applications, but I was pretty sure this built-in was something to do with "Text Messages" that you find in Shared Components. Problem is, when I go to "Text Messages" there is not one predefined message. Can I still customise the message?
Short answer - Yes.
There is a website available where you can review built-in text messages, as well as install other languages into your application. So, what we want to do is search for our message on this website. So first step, go to the website and navigate just to the original English translation.
Once here, we can search our message. We want to exclude the parameters from the error message in our search. Therefore, I ended up just searching for the text: "does not match format". Here we will find the message:
Message Code: APEX.DATEPICKER_VALUE_INVALID
Message: #LABEL# does not match format %0.
The message code is the key bit of information.
Back over to our APEX application now, if we navigate to Shared Components, Text Messages. Let's create a new message - we specify the name as the message code that we determined in the previous step. Of course in the message, we set it to the new validation we want to show when the built-in validation fails.
For the example in this article, let's enhance the message to include a sample input - #LABEL# does not match format %0. e.g. 01-JAN-2020.
With that set, our validation message now shows like so:
Thanks to Martin D'Souza for pointing me in the right direction on this.