You already learned how to use HTML forms to gather user input (see Chapter 26, "Retrieving and Formatting Data"). This chapter shows you how to use the cfform
tag to create dynamic forms in CFML. In addition to standard HTML form controls, the cfform
tag allows you to create forms that contain the following controls:
Most cfform
controls offer input validation attributes that you can use to validate user entry, selection, or interaction. This means you do not have to write separate CFML code specifically for input validation, as you do in HTML forms.
ColdFusion dynamically generates HTML forms from cfform
tags and passes to the browser any HTML code that it finds in the form. As a result, you can also do the following:
passthrough
attribute of the cfform
, cfinput
, and cfselect
tags to enter any HTML attributes that are not explicitly allowed in these tags. The attribute values are passed through to the HTML generated by these form tags.
form
tags with cfform
and your forms will work fine.cfform
the variable Form.fieldnames which contains the names of the form fields submitted from the form.
The following table describes the ColdFusion controls that you use in forms created using cfform
. You can use these tags only inside a cfform
tag.
The cfform
attribute preservedata
tells ColdFusion to continue displaying the data that a user entered in the form after the user submits the form. Data is preserved in the cfinput
, cfslider
, cftextinput
, and cftree
controls and in cfselect
controls populated by queries. If you specify a default value for a control, and a user overrides that default in the form, the user input is preserved.
You can retain data on the form when the form's action posts to the same ColdFusion page as the form itself, and the control names are the same.
For example, if you save this form as preserve.cfm, it continues to display any text that you enter after you submit it, as follows:
<cfform action="preserve.cfm" preservedata="Yes">
<p>Please enter your name: <cfinput type="Text" name="UserName" required="Yes"><p> <input type="Submit" name=""> <input type="RESET"> </cfform>
When using the preservedata
attribute, follow these guidelines:
cftree
, the preservedata
attribute causes the tree to expand the tree to the previously selected element. For this to work correctly, you must also set the completePath
attribute to True.
preservedata
attribute has no effect on cfgrid
. If you populate the control from a query, you must update the data source with the new data (typically by using cfgridupdate
) before redisplaying the grid. The grid then displays the updated database information.
The applet-based controls for cfform
-cfgrid
, cfslider
, cftextinput
, and cftree
-use JavaScript and Java to display their content. To allow them to display consistently across a variety of browsers, these applets use the Java plug-in. As a result, they are independent of the level of Java support provided by the browser.
ColdFusion downloads and installs the browser plug-in if necessary. Some browsers display a single permission dialog box asking you to confirm the plug-in install. Other browsers, particularly older versions of Netscape, require you to navigate some simple option screens.
Because the controls use JavaScript to return data to ColdFusion, if you disable JavaScript in your browser, it cannot properly run forms that contain these controls. In that case, the controls still display, but data return and validation does not work and you can receive a JavaScript error.
Because Java is handled by the plug-in and not directly by the browser, disabling Java execution in the browser does not affect the operation of the controls. If for some other reason, however, the browser is unable to render the controls as requested, a "notsupported" message appears in place of the control.
You can use the cfform
tag's notsupported
attribute to specify an alternate error message.