ColdFusion generates an error if you try to use a variable value that does not exist. Therefore, before you use any variable whose value is assigned dynamically, you must ensure that a variable value exists. For example, if your application has a form, it must use some combination of requiring users to submit data in fields, providing default values for fields, and checking for the existence of field variable values before they are used.
There are several ways to ensure that a variable exists before you use it, including the following:
- You can use the IsDefined function to test for the variable's existence.
- You can use the cfparam tag to test for a variable and set it to a default value if it does not exist.
- You can use a cfinput tag with a hidden attribute to tell ColdFusion to display a helpful message to any user who does not enter data in a required field. For more information on this technique, see Requiring users to enter values in form fields in the Working with action pages.
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 function. To check whether a specific entry exists in an array, use the ArrayIsDefined function. To check whether a specific key exists in a structure, use the StructKeyExists function.
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: