Testing For A Variable's Existence; Using The Cfparam Tag - MACROMEDIA COLDFUSION MX 61-DEVELOPING COLDFUSION MX Develop Manual

Developing coldfusion mx applications
Table of Contents

Advertisement

You can use a
message to any user who does not enter data in a required field. For more information on this
technique, see

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
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
marks. For more information on the
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
function.
IsDefined
You can also determine which Form variables exist by inspecting the contents of the
Form.fieldnames
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
bracket notation. For example,
element
myArray[3]
to a simple variable and use the

Using the cfparam tag

You can ensure that a variable exists by using the
existence and optionally supplies a default value if the variable does not exist. The
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
want the validation test to proceed:
tag with a
cfform
input
Chapter 26, "Requiring users to enter values in form fields," on page
function.
IsDefined
built-in variable. This variable contains a list of all the fields submitted by the
function always returns False if you specify an array or structure element using
IsDefined("myArray[3]")
has a value. To check for the existence of an array element, copy the element
IsDefined
cfparam
attribute to tell ColdFusion to display a helpful
hidden
IsDefined
function, see CFML Reference.
StructKeyExists
always returns False, even if the array
function to test whether the simple variable exists.
tag, which tests for the variable's
cfparam
tag to test for variable existence, depending on how you
function in double-quotation
function instead of the
cfparam
Ensuring variable existence
566.
tag
79

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion mx

Table of Contents