System information

110 Chapter 10: Lesson 7: Validating Data to Enforce Business Rules
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 Developers Guide
or CFML Reference.
Ensuring that a value was entered
The first approach that you take to enforce Compass Travel business rules is to enhance the 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 rules.
You can use the
cfif tag to create conditions that evaluate to either True or False. To use the cfif
tag to test whether a trip name was entered (business rule 1) on the Trip Edit form, you add the
following
cfif statement:
<cfif Form.tripName EQ "">
<cfoutput> Trip Name cannot be blank. </cfoutput>
</cfif>
In this 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 the message "Trip name
cannot be blank" to the browser.
Note: The keyword EQ is an operator that tests for equality. For more information about the cfif tag
and its operators, see ColdFusion MX Developer’s Guide.
To ensure that a Trip Name was entered:
1.
Open the tripeditaction.cfm file.
2.
Enter the following code after the line <cfset isOk = "Yes">.
<!--- Trip Name is required. --->
<cfif Form.tripName EQ "">
<CFSET IsOk = "No">
<cfoutput>Trip name cannot be blank.<br></cfoutput>
</cfif>
3.
Save the file.
To test the data validation:
1.
Open the tripedit.cfm page in a browser.
2.
Verify that there is no value in the Trip Name field.
3.
Click Save.
The error message “Trip name cannot be blank” appears.