User Guide
Developing code to validate data and enforce business rules 103
The cfselect tag
The
cfselect tag is an improved version of the HTML select tag. Like other ColdFusion form
tags, the
cfselect tag provides the required and message attributes that validate the data
entered. Using the
cfselect tag and the preceding cfquery, you can implement the eventType
field data entry as follows:
<!--- Field: eventType --->
<tr>
<td valign="top">Type of Event
</td>
<td>
<cfselect size="1" name="eventType" required="Yes"
message="Type of event must be selected.">
<cfoutput query="GetEvents">
<option value="#GetEvents.eventTypeID#">
#GetEvents.eventType#
</option>
</cfoutput>
</cfselect>
</td>
</tr>
Exercise: use eventtypes table to load event types
Do the following steps to modify the Trip Edit page to display a list of event types from the
eventtypes table and add validation.
To display a list of event types from the eventtypes table and add validation:
1 View the tripedit.cfm page in a browser. Select the event types drop-down list. Notice that only
three event types appear in the list.
2 Open the tripedit.cfm in the my_app directory in your editor.
3 Add the following code above the <html> tag:
<cfquery name="GetEvents" datasource="CompassTravel">
SELECT eventType, eventTypeID
FROM eventtypes
</cfquery>
4 Replace the following eventtypes code lines:
<cfselect size="1" name="eventType" required="Yes"
message="Type of event must be selected.">
<option value="1" selected>Surfing</option>
<option value="2">Mountain Climbing</option>
<option value="3">Mountain Biking</option>
</cfselect>
with these lines:
<cfselect size="1" name="eventType" required="Yes"
message="Type of event must be selected.">
<cfoutput query="GetEvents">
<option value="#GetEvents.eventTypeID#">
#GetEvents.eventType#
</option>
</cfoutput>
</cfselect>