Macromedia colfusion mx 7-getting started building coldfusion mx applications User Manual

Page of 152
Exercise 4: Providing server-side validation
109
Exercise 4: Providing server-side validation
In this exercise, you will learn about the following tasks:
Creating a local variable to indicate whether data entered in the form conforms to the 
Compass Travel business rules
Ensuring that a value was entered
Evaluating check box and radio button variables
Adding validation for all Compass Travel business rules
Creating a local variable
The purpose of the tripeditaction.cfm action page is to update the Compass Travel database, so it 
is important to make certain that any values entered conform to all the business rules before 
modifying the database. Failure of any one of the rules prevents modification of the database. 
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 whether 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 this example, the 
cfset
 tag 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. 
To create the local variable:
1.
Open the tripeditaction.cfm file in an editor.
2.
Add the following code at the top of the file:
<cfset isOk = "Yes">
3.
Modify the file by adding the following highlighted code:
<cfif isOk EQ "Yes">
<h1>Trip Added</h1>
<!--- Database insert logic goes here. --->
<cfoutput>
You have added #Form.TripName# to the trips database.
</cfoutput>
</cfif>
4.
Save the file.