Ensuring That Variables Exist; Using Cfparam To Test For Variables And Set Default Values - MACROMEDIA COLDFUSION 5-DEVELOPING Develop Manual

Table of Contents

Advertisement

88

Ensuring that Variables Exist

The sample code in the previous sections is incomplete. Either the form or the action
page should make sure that the SelectDepts variable has a value before it is used in
the SQL Select statement. Otherwise, users who do not select any department get an
error message. There are several ways to ensure that a variable exists before you use
it:

Using cfparam to test for variables and set default values

One way to ensure a variable exists is to use the
variable's existence and optionally supplies a default value if the variable does not
exist. The following code shows the syntax of the
<cfparam name="VariableName"
Note
For information on using the
the "Using cfparam to validate the data type" section of this chapter.
There are two ways to use the
on how you want the validation test to proceed:
The following example shows how to use the
an optional variable and to set a default value if the variable does not already exist:
<cfparam name="Form.Contract" default="Yes">
Example: Testing for variables
Using
page or a custom tag expects to receive before processing can proceed. This can
make your code more readable, as well as easier to maintain and debug.
You can use the
IsDefined
variable's existence," in Chapter 4.
You can use the
cfparam
does not exist.
You can use a form
input
a helpful message to any user who does not enter data in a required field.
type="data_type"
default="DefaultValue">
With only the
attribute to test that a required variable exists. If it does not
name
exist, the ColdFusion Server stops processing the page and displays an error
message.
With the
and
name
default
variable. If the variable exists, processing continues and the value is not changed.
If the variable does not exist, it is created and set to the value of the
attribute, and processing continues.
with the
cfparam
function, as described in the section "Testing for a
tag to test for a variable and set it to a default value if it
tag with a
hidden
attribute to validate the parameter data type, see
type
tag to test for variable existence, depending
cfparam
attributes to test for the existence of an optional
variable is one way to clearly define the variables that a
name
Chapter 6 Making Variables Dynamic
attribute to tell ColdFusion to display
tag, which tests for the
cfparam
tag:
cfparam
tag to check for the existence of
cfparam
default

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion 5

Table of Contents