|
Handling invalid data
How you handle invalid data depends
on the validation type. The information here describes validation
error-handling rules and considerations. For detailed information
on error handling in ColdFusion, including invalid data handling,
see Handling Errors.
For
onBlur, onSubmit, or onServer validation, you can use the cfinput or cftextarea tag’s message attribute
to specify a text-only error message to display. Otherwise, ColdFusion
uses a default message that includes the name of the form field
that was invalid. (For OnServer validation, you can customize this
message, as described in Handling form field validation errors.) The following example displays
an error message when the user enters an invalid e-mail address:
E-mail: <cfinput type="text" size="25" name="email"
validate="email" message="You must enter a valid e-mail address.">
For
hidden form validation, you can specify a text-only error message
in the hidden field’s value attribute. Otherwise,
ColdFusion uses a default message that includes the name of the
form field that was invalid. (You can customize this message, as
described in Handling form field validation errors.) The following cfinput tag,
for example, uses a hidden field validation to display an error
message if the user enters an invalid address. (It uses onServer validation
to display a different error message if the user fails to enter
a number.)
Telephone: <cfinput type="text" size="20" name="telephone"
validateat="onServer" required="Yes"
message="You must enter a telephone number">
<cfinput type="hidden" name="telephone_cfformtelephone"
value="The number you entered is not in the correct format.<br>Use a
number such as (617) 555-1212, 617-555-1212, or 617-555-1212 x12345">
For HTML and XML format forms (using ColdFusion skins), most
ColdFusion form tags have an onError attribute
that lets you specify a Javascript function to run if an onSubmit
error occurs.
For the IsValid function,
you write separate code paths to handle valid and invalid data.
The following example shows a simplified case that displays an error
message if the user entered an invalid e-mail address, or a different message
if the address is valid:
<cfif IsValid("email", custEmail)>
Thank you for entering a valid address.
<!--- More processing would go here. --->
<cfelse>
You must enter a valid e-mail address.<br>
Click the Back button and try again.
</cfif>
For cfparam and cfargument tags,
you use standard ColdFusion error-handling techniques. You can include
the tag in a try block and use a catch block
to handle the error, or you can use a custom error-handling page.
The following example form action page code uses a custom error
page, expresserr.cfm, to handle the error that the cfparam tag
generates if a user submits a form with an invalid e-mail address:
<cferror type="EXCEPTION" exception="expression" template="expresserr.cfm">
<cfif IsDefined("form.fieldnames")>
<cfparam name="form.custEmail" type="email">
<!--- Normal form processing code goes here. --->
</cfif>
|