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.

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. An error occurs if the action page tries to use a variable that was not passed.

To ensure that an error does not occur, you use the IsDefined function in a cfif statement to determine whether the user selected the Deposit Required check box on the form.

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. A value of No indicates that a deposit is not required; a value of Yes indicates that a deposit is required.

To set the value of the Deposit Required check box:

  1. Open the tripeditaction.cfm file.
  2. Enter the following code after the first cfif code block.
    <cfif not IsDefined("Form.depositRequired")>
    	<cfset form.depositRequired = "No">
    </cfif>
    
  3. Save the file.

Adding validation for all Compass Travel business rules

You add validation for all the form variables.

To validate all the data passed:

  1. Open the tripeditaction.cfm file in the my_app directory.
  2. Enter the validation code blocks in the following table after the cfif code blocks that you already entered.
  3. Save the file.

The following table lists the Compass Travel business rules and shows the corresponding code that ensures that the data entered in the form follows the business rules.

Note: You have already entered validation code for business rule 1. Validation code for business rule 10 is described in more detail later in this lesson.

Rule

Description

Validation code

2

All trips must be accompanied by a full description.

<!--- Trip description is required. --->
<cfif Form.tripDescription EQ "">
	<cfset IsOk = "No">
	<cfoutput>
		Trip description cannot be blank.
	</cfoutput>
</cfif>

3

Each trip must be categorized by event type. Only valid event types (1-surfing, 2-mountain climbing, and so on) are permissible.

Because event type 1 (surfing) is selected by default, there is always a value for event type.

4

Trip locations are required.

<!--- Trip location is required. --->
<cfif Form.tripLocation EQ "">
	<cfset IsOk = "No">
	<cfoutput>
		Trip location cannot be blank.
	</cfoutput>
</cfif>

5

The maximum number of people permitted on the trip must be specified.

<!--- Number of people is required and must be numeric. --->
<cfif Form.numberPeople EQ "" or
	IsNumeric(Form.numberPeople) EQ False>
	<cfset IsOk = "No">
	<cfoutput>
		The number of people must be a number and
		cannot be blank.
	</cfoutput>
</cfif>

6

The trip departure and return dates must be specified for each trip.

All trip dates must be valid future dates. Departure date must precede return date.

<cfif form.departureDate GT form.returnDate>
	<cfset isOk = "No">
	<cfoutput>
		Departure date cannot precede return date.
		Please re-enter.
	</cfoutput>
</cfif>

7

The trip’s 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>
	<!--- Price must be 20% greater than Base Cost. --->
	<cfif Form.basecost * 1.2 GT  #Form.price#>
		<cfset IsOk = "No">
		<cfoutput>
			Price must be marked up at least 20%
			above cost.
		</cfoutput>
	</cfif>
</cfif>

8

Any trip priced over $750 requires a deposit.

<!--- Price is required and must be numeric. --->
<cfif Form.price EQ "" or
	IsNumeric(Form.baseCost) EQ False>
	<cfset IsOk = "No">
	<cfoutput>
		Price must be a number and cannot be blank.
	</cfoutput>
	<cfelse>
	<!--- A deposit is required when the price is > $750. --->
		<cfif Form.price GT 750 AND
			Form.depositRequired EQ "No">
			<cfset IsOk = "No">
			<cfoutput>
				Deposit is required for 
				trips priced over $750.
			</cfoutput>
	</cfif>
</cfif>

9

A trip leader must be identified.

<!--- Trip Leader is required. --->
<cfif Form.tripLeader EQ "">
	<cfset IsOk = "No">
	<cfoutput>
		A trip leader must be specified.
	</cfoutput>
</cfif>

10

A photo must accompany all new trips.

<!--- Photo filename is required. --->
<cfif Form.photo EQ "">
	<cfset IsOk = "No">
	<cfoutput>
		Photo filename must be specified.</cfoutput>
</cfif>

Note: The code for business rules 7 and 8 uses ColdFusion cfif and cfelse conditional processing tags. The code inside the cfif tags only executes when the condition evaluates to True. To perform other actions when the condition evaluates to False, the cfelse tag is used. For more information about using conditional processing tags, see ColdFusion MX Developer’s Guide.

To test the validation code:

  1. View the tripedit.cfm page in the browser.
  2. In the form, enter 500 in both the Price and Base Cost fields.
  3. Click the Save button.

    The trip price error message displays: "Price must be marked up at least 20% above cost."

  4. Click the browser Back button to return to the tripedit.cfm page.
  5. To avoid the error, enter 800 in the Price field, and click Save.
  6. Test various combinations to ensure that all the Compass Travel business rules are enforced by filling out the fields on the form and clicking Save.

    Testing recommendations: