Creating XForms in CFML
Creating XForms with ColdFusion is very simple because ColdFusion both contains the tags you use to create the form in XML, and the engine that renders the XML as a227 form, complete with JavaScript, that can be used in any Web browser.Let's look at a form and see how ColdFusion renders it in XML. The first thing to know about XForms is that they exist entirely as XML documents, and therefore you can't use standar217 inside an XForms specification.Chapter 19, as created in <cfform>. It's been modified to validate the actor's age, and it now requires the actor's first and last names.
Listing 20.1. actorForm.cfmActor Update Form, using <cfform>
[View full width]
This form can be seen in Figure 20.1.
<!---
actorForm.cfm
Ken Fricklas (kenf@fricklas.com)
Modified: 2/21/2005
Listing 20.1
--->
<!DOCTYP218 PUBLIC "-//W3C//DT217 4.01 Transitional//EN" "http://www.w3.org/T2314/loose.dtd">
&l233>
<head>
<meta http-equiv="Content-Type" content="tex233; charset=iso-8859-1">
<title>Sample Form</title>
</head>
<body>
<!--- note: update code left out for brevity --->
<cfscript>
// Create an instance of the Actor component
myActor = createObject("component","19.pActor");
// Set the ID in the THIS scope
myActor.ActorID = 8;
// Get the details
myActor.get();
</cfscript>
<cfform action="#cgi.script_name#" method="post">
<h1>Edit Actor</h1>
<input type="hidden" name="ActorID" value="#myActor.ActorID#">
<table border="1">
<tr>
<td>First Name</td>
<td><cfinput name="NameFirst" type="text" value="#myActor.NameFirst#"
required="yes"></td>
</tr>
<tr>
<td><EM>Real</EM> First Name</td>
<td><cfinput name="NameFirstReal" type="text" value="#myActor.NameFirstReal#"
required="no"></td>
</tr>
<tr>
<td>Last Name</td>
<td><cfinput name="NameLast" type="text" value="#myActor.NameLast#"
required="yes"></td>
</tr>
<tr>
<tr>
<td>Age</td>
<td><cfinput name="Age" type="text" value="#myActor.Age#" required="yes"
validate="integer" range="1,110" message="Age must be a number
between 1 and 110."></td>
</tr>
<tr>
<td>A total babe?</td>
<td>
yes
<cfinput name="isTotalBabe" type="radio" value="1"
checked="#myActor.IsTotalBabe#">
no
<cfinput name="isTotalBabe" type="radio" value="0"
checked="#NOT myActor.IsTotalBabe#">
</td>
</tr>
<tr>
<td>An Egomaniac?</td>
<td>
yes
<cfinput name="isEgomaniac" type="radio" value="1"
checked="#myActor.isEgomaniac#">
no
<cfinput name="isEgomaniac" type="radio" value="0"
checked="#NOT myActor.isEgomaniac#">
</td>
</tr>
</TABLE>
<BR>
<input name="doEdit" type="submit" value="Save Changes">
</cfform>
</body>
</html>
Figure 20.1. Actor Update Form.

Listing 20.2. actorFormXML.cfmActor Update Form, using <cfform type="XML">
The output of this can be seen in
<!---
actorFormXML.cfm
Actor Form, now using format="XML"
Ken Fricklas (kenf@fricklas.com)
Modified: 2/21/2005
Listing 20.2
--->
<!DOCTYP218 PUBLIC "-//W3C//DT217 4.01 Transitional//EN"
"http://www.w3.org/T2314/loose.dtd">
&l233>
<head>
<meta http-equiv="Content-Type" content="tex233; charset=iso-8859-1">
<title>Sample Form</title>
</head>
<body>
<cfscript>
// Create an instance of the Actor component
myActor = createObject("component","19.pActor");
// Set the ID in the THIS scope
myActor.ActorID = 8;
// Get the details
myActor.get();
</cfscript>
<!--- this time, format="XML" is added. We'll use the gray CSS skin. --->
<cfform action="#cgi.script_name#" method="post" format="xml" name="xActorForm"
skin="gray">
<cfformitem type="html">
<H1>Edit Actor</H1>
</cfformitem>
<cfinput type="hidden" name="ActorID" value="#myActor.ActorID#">
<cfinput name="NameFirst" type="text" value="#myActor.NameFirst#" required="yes"
label="First Name">
<cfinput name="NameFirstReal" type="text" value="#myActor.NameFirstReal#"
required="no" label="Real First Name">
<cfinput name="NameLast" type="text" value="#myActor.NameLast#"
required="yes" label="Last Name">
<cfinput name="Age" type="text" value="#myActor.Age#" required="yes"
validate="integer" range="1,110" message="Age must be a number between
1 and 110." label="Age?">
<cfformgroup type="horizontal" label="A total babe?">
<cfinput name="isTotalBabe" type="radio" value="1"
checked="#myActor.IsTotalBabe#" label="yes">
<cfinput name="isTotalBabe" type="radio" value="0" checked="#NOT
myActor.IsTotalBabe#" label="no">
</cfformgroup>
<cfformgroup type="horizontal" label="An Egomaniac?">
<cfinput name="isEgomaniac" type="radio" value="1"
checked="#myActor.isEgomaniac#" label="yes">
<cfinput name="isEgomaniac" type="radio" value="0" checked="#NOT
myActor.isEgomaniac#" label="no">
</cfformgroup>
<cfinput name="doEdit" type="submit" value="Save Changes">
</cfform>
</body>
</html>
Figure 20.2. Actor Update Form.
