Ensuring variable existence
79
•
You can use a
cfform
input
tag with a
hidden
attribute to tell ColdFusion to display a helpful
message to any user who does not enter data in a required field. For more information on this
technique, see
Chapter 26, “Requiring users to enter values in form fields,” on page 566
.
Testing for a variable’s existence
Before relying on a variable’s existence in an application page, you can test to see if it exists by
using the
IsDefined
function.
For example, if you submit a form with an unsettled check box, the action page does not get a
variable for the check box. The following example from a form action page makes sure the
Contractor check box Form variable exists before using it:
<cfif IsDefined("Form.Contractor")>
<cfoutput>Contractor: #Form.Contractor#</cfoutput>
</cfif>
You must always enclose the argument passed to the
IsDefined
function in double-quotation
marks. For more information on the
IsDefined
function, see
CFML Reference
.
If you attempt to evaluate a variable that you did not define, ColdFusion cannot process the page
and displays an error message. To help diagnose such problems, turn on debugging in the
ColdFusion MX Administrator or use the debugger in your editor. The Administrator debugging
information shows which variables are being passed to your application pages.
Variable existence considerations
If a variable is part of a scope that is available as a structure, you might get a minor performance
increase by testing the variable’s existence using the
StructKeyExists
function instead of the
IsDefined
function.
You can also determine which Form variables exist by inspecting the contents of the
Form.fieldnames
built-in variable. This variable contains a list of all the fields submitted by the
form. Remember, however, that form text fields are always submitted to the action page, and
might contain an empty string if the user did not enter data.
The
IsDefined
function always returns False if you specify an array or structure element using
bracket notation. For example,
IsDefined("myArray[3]")
always returns False, even if the array
element
myArray[3]
has a value. To check for the existence of an array element, copy the element
to a simple variable and use the
IsDefined
function to test whether the simple variable exists.
Using the cfparam tag
You can ensure that a variable exists by using the
cfparam
tag, which tests for the variable’s
existence and optionally supplies a default value if the variable does not exist. The
cfparam
tag
has the following syntax:
<cfparam name="
VariableName
"
type="
data_type
"
default="
DefaultValue
">
Note:
For information on using the
type
attribute to validate the parameter data type, see
CFML
Reference
.
There are two ways to use the
cfparam
tag to test for variable existence, depending on how you
want the validation test to proceed:
Summary of Contents for COLDFUSION MX 61-DEVELOPING COLDFUSION MX
Page 1: ...Developing ColdFusion MX Applications...
Page 22: ...22 Contents...
Page 38: ......
Page 52: ...52 Chapter 2 Elements of CFML...
Page 162: ......
Page 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Page 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Page 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Page 266: ......
Page 314: ...314 Chapter 14 Handling Errors...
Page 344: ...344 Chapter 15 Using Persistent Data and Locking...
Page 349: ...About user security 349...
Page 357: ...Security scenarios 357...
Page 370: ...370 Chapter 16 Securing Applications...
Page 388: ...388 Chapter 17 Developing Globalized Applications...
Page 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Page 410: ......
Page 426: ...426 Chapter 19 Introduction to Databases and SQL...
Page 476: ...476 Chapter 22 Using Query of Queries...
Page 534: ...534 Chapter 24 Building a Search Interface...
Page 556: ...556 Chapter 25 Using Verity Search Expressions...
Page 558: ......
Page 582: ...582 Chapter 26 Retrieving and Formatting Data...
Page 668: ......
Page 734: ...734 Chapter 32 Using Web Services...
Page 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Page 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Page 788: ......