[Tip of the Week] Defaulting Values in the Request Collection

The request collection is a struct of values which are aggregated from the FORM and URL scopes as well as remote parameters to proxy requests. The data in your request collection is available via methods in the event object or by referencing the “rc” struct directly.


What do you do when you need to deal with item in the rc (or request collection) that might not exist? Common scenarios would be check boxes that may be left blank, or optional URL parameters. In those instances you may want to provide a default value, or simply test for the existence of the item.

The first thing you might think to do would be typical manipulation of the rc struct. Remember, structs are passed by reference, so any changes made to rc in an event handler for instance, are made for the remainder of that request.

// Modify the rc with a default value
param name=‘rc.foo’ default=‘bar’;

// Check for the existence of a key

// return a default without modifying the rc
if(structKeyExists(rc,‘foo’)) {
return rc.foo;
} else {
return ‘default’;

Those methods will work, but there are cleaner, more consistent ways to accomplish the same end using the request context, or event object.

// Modify the rc with a default value

// Check for the existence of a key

// return a default without modifying the rc

Hopefully these simple but effective helper methods in the request context will make productive additions to your toolbox.

Learn everything the event object can do for you with the API docs: http://www.coldbox.org/documents/api/ColdBoxDocs-3.5.0/index.html?coldbox/system/mvc/core/RequestContext.html

P.S. Other useful methods:



ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com