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.

Because you did not yet add any logic to test whether any values entered in the form are valid, the page works precisely as before, displaying a message that indicates the database was updated, without actually performing the update.

For more information about using the cfset and cfif tags, see ColdFusion MX Developer’s Guide or CFML Reference.