User Guide

Developing code to validate data and enforce business rules 97
Evaluating check box and radio button variables
Business rule 8 in the Compass Travel new trip policy requires you to test the value of the
depositRequired check box form variable. Check box and radio button variables are only passed
to the action page when the user selects these options on the form. Therefore, an error occurs if
the action page tries to use a variable that was not been passed.
To insure an error does not occur, you will use the
IsDefined function in a cfif statement to
determine whether the user selected the Deposit Required check box option on the form:
<cfif not IsDefined("Form.depositRequired")>
<cfset form.depositRequired = "No">
</cfif>
The cfif statement and the IsDefined function evaluate the value of the form variable
depositRequired to determine if a value exists. The statement not IsDefined returns True if
the specified variable is not found and the
cfset statement sets the form variable to No. No
indicates a deposit is not required; Yes indicates a deposit is required.
Evaluating whether business rules fail
The purpose of the tripeditaction.cfm action page is to update the Compass Travel database, so it
is important to make certain that all the business rules are passed successfully before the database
insert is executed. Failure of any one of the rules negates the insert.
One approach to ensuring that the action page considers each business rule is to create a local
variable with a
cfset tag within the action page that tests to see if any of the business rules failed.
The
cfset tag lets you manipulate the value of a variable. For example, the following pseudocode
initializes a variable to a specific value and checks the value using the
cfif statement:
<cfset isOk = "Yes">
if rule 1 fails then
<cfset isOK = "No"
...
if Rule n fails then
<cfset isOk = "No">
...
<cfif isOk = "Yes">
update the database
</cfif>
In the previous example, cfset initializes the local variable isOk to Yes. If any rule fails, the
variable
isOK is set to No. The code then tests if isOk equals Yes, before executing the SQL insert
logic.
For more information about using the
cfset and cfif tags and the IsDefined function, see
Developing ColdFusion MX Applications or CFML Reference.
Exercise: create an action page with server-side validation
In this exercise you build an action page (tripeditaction.cfm)to validate the data passed to
ColdFusion MX from the tripedit.cfm data entry page. You use the
cfif and cfset tags to build
edits that ensure the data passed is valid per the Compass Travel business rules. Additionally, you
will use the ColdFusion
IsDefined function to check to see if data was entered in the data entry
form (tripedit.cfm).