System information
Exercise 4: Providing server-side validation 111
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.