Programming instructions

Developing code to validate data and enforce business rules 105
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 with CFML or the 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
the ColdFusion Server from the tripedit.cfm data entry page. You use the
cfif and
c
fset 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).
To build trip edit action page and validate data passed:
1 Open an editor and create a new page called tripeditaction.cfm in the my_app
directory. The new page appears as follows:
<html>
<head>
<title>Untitled</title>
</head>
<body>
</body>
</html>
2 To ensure that Compass Travel business rule 7 is met, insert the following code above
the
<html> tag on the tripeditaction.cfm page. For your convenience, business rule 7
is repeated.
Business rule 7: The trips price and base cost are required. Both values are positive
numeric values. The trip price must have at least a 20% markup over base cost.
<!--- Base Cost is Required and must be Numeric --->
<cfif Form.baseCost EQ "" or IsNumeric(Form.baseCost) EQ False>
<cfset IsOk = "No">
<cfoutput>Base cost must be a number and cannot be blank.</cfoutput>
<cfelse>