User Guide

Ensuring that Variables Exist 89
For example, the following series of cfparam tags indicates 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 Chapter 11, Preventing and
Handling Errors on page 191.
Example: setting default values
This example uses cfparam to see if optional variables exist. If they do exist,
processing continues. If they do not exist, they are created and set to the
default
value.
<cfparam name="Cookie.SearchString" default="temple">
<cfparam name="Client.Color" default="Grey">
<cfparam name="ShowExtraInfo" default="No">
You can use cfparam to set default values for URL and Form variables, instead of
using conditional logic. For example, the action page could have the following code
to ensure that a SelectDepts variable exists:
<cfparam name="Form.SelectedDepts" default="Marketing,Sales">
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 provides a server-side mechanism for requiring users to
enter data in fields.
To require entry in an input field, use a hidden field that has a
name attribute
composed of the field name and the suffix "_required." For example, to require that
the user enter a value in the FirstName field, use the syntax:
<input type="hidden" name="FirstName_required">
If the user leaves the FirstName field empty, ColdFusion rejects the form submittal
and returns a message informing the user that the field is required. You can
customize the contents of this error message using the
value attribute of the hidden
field. For example, if you want the error message to read You must enter your first
name, use the following syntax:
<input type="hidden"
name="FirstName_required"
value="You must enter your first name.">