User Guide

Table Of Contents
616 Chapter 26: Introduction to Retrieving and Formatting Data
Testing for a variable’s existence
Before relying on a variables existence in an application page, you can test to see if it exists using
the ColdFusion
IsDefined function. A function is a named procedure that takes input and
operates on it. For example, the
IsDefined function determines whether a variable exists. CFML
provides a large number of functions, which are documented in CFML Reference.
The following code prevents the error in the previous example by checking to see whether the
Contractor Form variable exists before using it:
<cfif IsDefined("Form.Contractor")>
<cfoutput>Contractor: #Form.Contractor#</cfoutput>
</cfif>
The argument passed to the IsDefined function must always be enclosed 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. The Administrator debugging information shows which variables
are being passed to your application pages.
Requiring users to enter values in form fields
One of the limitations of HTML forms is the inability to define input fields as required. Because
this is a particularly important requirement for database applications, ColdFusion lets you require
users to enter data in fields. To specify a field as required, you can do either of the following:
Use the required attribute of the cfinput, cfselect, cftextarea, and cftree tags.
Use a hidden field that has a name attribute composed of the field name and the suffix
"_required". You can use this technique with CFML and HTML form tags.
For example, to require that the user enter a value in the FirstName field of a
cfinput tag, use the
following syntax:
<cfinput type="Text" name="FirstName" size="20" maxlength="35" required="Yes">
To require that the user enter a value in the FirstName field of an HTML input tag, use the
following syntax:
<input type="Text" name="FirstName" size="20" maxlength="35">
<input type="hidden" name="FirstName_required">
</cfoutput>
Closes the cfoutput block.
<br>
<cfoutput>Contractor: #Form.Contractor#
</cfoutput>
Displays a blank line followed by the text “Contractor”:
and the value of the form Contractor check box.
A more complete example would test to ensure the
existence of the variable and would use the variable in
the query.
Code Description