Programming instructions

104 Lesson 4 Validating Data to Enforce Business Rules
Validating data using a server-side action page
The first approach you will take to enforce Compass Travel business rules is to develop an
action page to validate the data collected on the data entry form. The action page receives
a form variable for every field on the form that contains a value. You use the
cfif tag to
test the values of these fields to ensure that they adhere to Compass Travel business
policy.
Using a cfif tag to enforce business rules
The
cfif tag lets you create conditions that evaluate to either True or False. Therefore,
to use the
cfif tag to test whether a trip name was entered (business rule 1)on the Trip
Edit form, you code the following
cfif statement:
<cfif Form.tripName EQ "">
<cfoutput> Trip Name cannot be blank. </cfoutput>
</cfif>
In the previous example, the cfif statement tests to see if the value of the form variable
tripName is blank. If the trip name condition evaluates to True, ColdFusion sends "Trip
name cannot be blank" to the browser.
Note: The keyword EQ is an operator used to test for equality. For more information about
the cfif tag and its operators, see Developing ColdFusion MX Applications with CFML.
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.