Programming instructions
106 Lesson 4 Validating Data to Enforce Business Rules
<!--- 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><br>
</cfif>
</cfif>
Note: The code for business rule 7 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
Developing ColdFusion MX Applications with CFML.
3 Save the page.
4 Use the following steps to test the code to see if it meets the objective of business rule
7:
a View the tripedit.cfm page in the browser.
b In the form, enter the number 500 in both the Price and Base cost fields.
c Click the Save button.
The trip price error message displays: "Price must be marked up at least 20%
above cost."
d Click the browser Back button to return to the tripedit.cfm page.
e To avoid the error, enter the number 800 in the Price field and click Save.
5 Complete all the business rules using server-side validation. You must insert the code
for each business rule above the
<html> tag. As an example, see the
tripeditaction1.cfm page in the solutions directory.
Tip: You can either modify your new tripeditaction.cfm page to include the code
necessary to meet all 10 business rules or you can copy the tripeditaction1.cfm page from
the solutions directory to tripeditaction.cfm in the my_app directory.
6 Test various combinations to make sure all the Compass Travel business rules are
enforced by filling out the fields on the form and clicking save.
Testing recommendations:
• Leave out required fields such as trip name or location.
• Enter in a non-numeric value in number of people such as one.
• Leave the entire form blank and click Save. The following messages appear:
Trip name cannot be blank. A trip leader must be specified. Photo file name must
be specified. The number of people must be a number and cannot be blank. Trip
location cannot be blank. Base cost must be a number and cannot be blank. Price
must be a number and cannot be blank.
Drawbacks of validating data on the server-side
Validating data on the server-side has two drawbacks. First, since the action page is used
for validation, the
form page is not in the browser context when the error is trapped. The
user will, therefore, not get immediate feedback from the page where the data was
entered. Second, because data capture occurs on the client and validation on the server,
the number of round-trips to the server is increased. This can cause increased traffic on