Skip to main content

APEX : How to emphasis Required Fields...


In APEX there is no property to mark a field as a required field (at this moment, could be an enhancement request!). However you can mark the prompt of a field by using the 'Required' template - with or without help. So why not use the APEX Repository to enhance the look-and-feel of your application and show the user which fields are required? And of course we use jQuery to accomplish this.

This is how you do it:
Create an On Demand Application Process (lets call it getRequiredItems) and fire it onLoad - After Header:

DECLARE
CURSOR c_items IS
SELECT item_name
FROM apex_application_page_items
WHERE item_label_template LIKE 'Required%'
AND page_id = :APP_PAGE_ID;

item_list varchar2(32000) := '';
item_sep varchar2(1) := '~';

BEGIN
FOR r_item IN c_items LOOP
item_list := item_list||r_item.item_name||item_sep;
END LOOP;
-- P0_REQUIRED_ITEMS is an hidden item on Page 0
:P0_REQUIRED_ITEMS := substr( item_list, 1, length( item_list ) -1);
END;
This process returns the items with a 'Required'-prompt for the current page in one long string, separated with a ~.

In Page 0, create a Javascript function that adds a class ('RequiredField') to the fields in the P0_REQUIRED_ITEMS string filled by the Application Process and (optional) adds an onblur event that calls a function (checkRequiredField) using

function setRequiredItems(){
var colArray = $v("P0_REQUIRED_ITEMS").split("~");
for(i = 0; i < colArray.length; i++){
$('#'+colArray[i]).addClass("RequiredField").attr("onblur",'checkRequiredField(this)');
}
}
Now you can create an entry in your CSS file to give the required field a nice color (or whatever else you want).
Next create the Javascript function to check if a required field is empty or not. If the field is empty, the user cannot navigate out of the field, unless he puts in a value.

function checkRequiredField(pThis){
if (!(pThis.value)){
alert('Field must be entered');
setTimeout(function(){pThis.focus();}, 5);
}
}

Now add this Javascript function to the jQuery document.ready function:

$(function(){
setRequiredItems();
});

And the result is something like the picture on the top of this post. A live example is available on apex.oracle.com.

Comments

Paulo Vale said…
Nice tip Roel ;)
Stew said…
Very nice!

Of course Apex's default templates add an * to each field with the Required label but this is much better, especially with the validation.

Now I'm not sure if I'd highlight the required fields that much, the yellow is a bit garish. But if your users need really obvious markers, so be it! :-)
Unknown said…
This comment has been removed by the author.
Roel said…
@Stew: Of course you can pick any color you like (or one or more other stuff you can set using the stylesheet).
Algis said…
Super, works fine :)
Thanks.
Patrick Wolf said…
Roel,

nice tip!

Just a question, why don't you immediately generate the string with the items in the page 0 region? That would avoid the additional AJAX call to the server to get the items. I think that should be much faster and would save database resources.

Greetings
Patrick
Roel said…
@Patrick: That is a good idea. I changed the example to work without an (extra) AJAX call. Why make things more complex than necessary....
Martin D'Souza said…
Hi Roel,

Nice post! I know Patrick suggested that you make a Page 0 process to load the list. To help avoid the extra processing why not update the "required" template with a certain class and use jQuery to tag all the fields with that class as required. That way you wouldn't even need to run the process?

Martin.
Roel said…
@Martin. I think you can do that, but there is only a 'Required' Label template. So you have to write jQuery code to get the 'required' label and then use the 'for' attribute to determine the actual field and tag those fields with that class.
Thanks for the update!
Robert said…
The highlight works great but the validation did not. I followed each step (process, page 0, javascripts) but the validation saying that "this field is required." did not happen. I also tried it using your example, and when I clicked outside the field (after I deleted the data) it did not give the error. Did I do something wrong?
Roel said…
@Robert,
I messed up that page (trying something else), but it is working again. Check the value of P0_REQUIRED_ITEMS in Session State (and check that jQuery is loaded!).

Popular posts from this blog

apex_application.g_f0x array processing in Oracle 12

If you created your own "updatable reports" or your custom version of tabular forms in Oracle Application Express, you'll end up with a query that looks similar to this one: then you disable the " Escape special characters " property and the result is an updatable multirecord form. That was easy, right? But now we need to process the changes in the Ename column when the form is submitted, but only if the checkbox is checked. All the columns are submitted as separated arrays, named apex_application.g_f0x - where the "x" is the value of the "p_idx" parameter you specified in the apex_item calls. So we have apex_application.g_f01, g_f02 and g_f03. But then you discover APEX has the oddity that the "checkbox" array only contains values for the checked rows. Thus if you just check "Jones", the length of g_f02 is 1 and it contains only the empno of Jones - while the other two arrays will contain all (14) rows. So for

Filtering in the APEX Interactive Grid

Remember Oracle Forms? One of the nice features of Forms was the use of GLOBAL items. More or less comparable to Application Items in APEX. These GLOBALS where often used to pre-query data. For example you queried Employee 200 in Form A, then opened Form B and on opening that Form the Employee field is filled with that (GLOBAL) value of 200 and the query was executed. So without additional keys strokes or entering data, when switching to another Form a user would immediately see the data in the same context. And they loved that. In APEX you can create a similar experience using Application Items (or an Item on the Global Page) for Classic Reports (by setting a Default Value to a Search Item) and Interactive Reports (using the  APEX_IR.ADD_FILTER  procedure). But what about the Interactive Grid? There is no APEX_IG package ... so the first thing we have to figure out is how can we set a filter programmatically? Start with creating an Interactive Grid based upon the good old Employ

Stop using validations for checking constraints !

 If you run your APEX application - like a Form based on the EMP table - and test if you can change the value of Department to something else then the standard values of 10, 20, 30 or 40, you'll get a nice error message like this: But it isn't really nice, is it? So what do a lot of developers do? They create a validation (just) in order to show a nicer, better worded, error message like "This is not a valid department".  And what you then just did is writing code twice : Once in the database as a (foreign key) check constraint and once as a sql statement in your validation. And we all know : writing code twice is usually not a good idea - and executing the same query twice is not enhancing your performance! So how can we transform that ugly error message into something nice? By combining two APEX features: the Error Handling Function and the Text Messages! Start with copying the example of an Error Handling Function from the APEX documentation. Create this function