
63
DEVELOPING
COLDFUSION 9 APPLICATIONS
The CFML Programming Language
Las
t
upda
te
d 8/5/2010
For example, the following
cfparam
tags indicate that this page expects two form variables named StartRow and
RowsToFetch:
<cfparam name="Form.StartRow">
<cfparam name="Form.RowsToFetch">
If the page with these tags is called without either one of the form variables, an error occurs and the page stops
processing. By default, ColdFusion displays an error message; you can also handle the error as described in “
Handling
Errors
” on page 275.
Example: setting default values
The following example uses the
cfparam
tag to see if optional variables exist. If they do exist, processing continues. If
they do not exist, the ColdFusion server creates them and sets them to the default values.
<cfparam name="Cookie.SearchString" default="temple">
<cfparam name="Client.Color" default="Gray">
<cfparam name="ShowExtraInfo" default="No">
You can use the
cfparam
tag to set default values for URL and Form variables, instead of using conditional logic. For
example, you could include the following code on the action page to ensure that a SelectedDepts variable exists:
<cfparam name="Form.SelectedDepts" default="Marketing,Sales">
Validating data
It is often not sufficient that input data merely exists; it must also have the right format. For example, a date field must
have data in a date format. A salary field must have data in a numeric or currency format. There are many ways to
ensure the validity of data, including the following methods:
•
Use the
cfparam
tag with the
type
attribute to validate a variable.
•
Use the IsValid function to validate a variable.
•
Use the
cfqueryparam
tag in a SQL WHERE clause to validate query parameters.
•
Use
cfform
controls that have validation attributes.
•
Use a form
input
tag with a
hidden
attribute to validate the contents of a form input field.
Note:
Data validation using the
cfparam,cfqueryparam
, and
form
tags is done by the server. Validation using
cfform
tags and hidden fields is done using JavaScript in the user’s browser, before any data is sent to the server.
For detailed information on validating data in forms and variables, see “
Validating Data
” on page 743 For detailed
information on validating query parameters, see “
Using cfqueryparam
” on page 416.
Passing variables to custom tags and UDFs
The following sections describe rules for how data gets passed to custom tags and user-defined functions that are
written in CFML, and to CFX custom tags that are written in Java or C++.
Passing variables to CFML tags and UDFs
When you pass a variable to a CFML custom tag as an attribute, or to a user-defined function as an argument, the
following rules determine whether the custom tag or function receives its own private copy of the variable or only gets
a reference to the calling page’s variable:
•
Simple variables and arrays are passed as copies of the data. If your argument is an expression that contains multiple
simple variables, the result of the expression evaluation is copied to the function or tag.