Programming instructions

108 Lesson 4 Validating Data to Enforce Business Rules
Client-side validation approach using ColdFusion form tag
The following code is on the client:
<cfinput name="duration" message="Duration must be a number and cannot be blank."
validate="integer" required="Yes" size="3" maxlength="3">
Exercise: modify Trip Edit page to exploit ColdFusion form tags
In this exercise, you will use the ColdFusion form tags to move the validation of many
business rules from the server to the client. To do this, you will change the HTML form
tags in the tripedit.cfm page to ColdFusion form tags that validate these fields on the
client side. Next, you will remove the unneeded server-side single-field validation code
from tripeditaction.cfm page. Finally, you will test the form to ensure the client side
validation is working correctly.
To exploit the ColdFusion form tags on the Trip Edit page:
1 Open the tripedit.cfm in the my_app directory in your editor.
2 Locate and change the
<form> and </form> tags to <cfform> and </cfform>,
respectively.
3 Change the <input> tags to
<cfinput> tags and <select> tags to <cfselect> tags.
Note that the input type for the Submit button must remain a standard input rather
than
cfinput.
4 For each ColdFusion form tag (
cfinput, and cfselect), assign the appropriate
values:
For example, the Trip Name field requires the following code:
<cfinput maxlength = "50" size = "50" required = "Yes" name= "tripName"
message = "Trip name must not be blank">
Code Explanation
<cfinput name="duration"
message="Duration must be a number and
cannot be blank." validate="integer"
required="Yes" size="3" maxlength="3">
Use the cfinput tag to create the duration
input entry field within a cfform. The validate
attribute defines the field as an integer. The
required attribute indicates that the field must
have an entry. If the data is not entered or data
entered is not an integer, the message attribute
specifies that the message, "Duration must be...."
appears.
Attribute value Description
required Use this attribute for fields that must be filled out or selected.
validate Use this attribute for fields that requires a specific data type for
validation. Values include: integer, date, time, telephone, and zip
code.
message Use this attribute for fields that require an error message to be
displayed if validation fails. The message reflects the text that
describes the business rule.