100
Chapter 8: Lesson 4: Validating Data to Enforce Business Rules
Server-side validation approach (no ColdFusion form tag)
The following code is on the server (tripeditaction.cfm page):
<!--- 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>
Client-side validation approach using ColdFusion form tag
The following code is on the client (tripedit.cfm):
<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
.
Code Explanation
<cfif Form.numberPeople EQ "" or
IsNumeric(Form.numberPeople) EQ
False>
The
cfif
tag evaluates the value of the form variable
numberPeople
to determine if the user entered a value.
The
IsNumeric
function checks whether the value
entered on the form was a numeric value.
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.
Summary of Contents for COLDFUSION MX 61-GETTING STARTED BUILDING COLDFUSION...
Page 1: ...Getting Started Building ColdFusion MX Applications...
Page 6: ...6 Contents...
Page 10: ......
Page 30: ...30 Chapter 2 CFML Basics...
Page 36: ...36 Chapter 3 Database Fundamentals...
Page 48: ......
Page 76: ...76 Chapter 6 Lesson 2 Writing Your First ColdFusion Application...
Page 134: ...134 Index...