It is often not sufficient that input data merely exists; it must also have the right format. For example, a date field must have data in a date format. A salary field must have data in a numeric or currency format. There are many ways to ensure the validity of data, including the following methods:
cfparam tag with the type attribute to validate any variable.
input tag with a hidden attribute to validate the contents of a form input field.cfform controls that have validation attributes. (For information on using cfform tags, see Chapter 9, "Building Dynamic Forms".)cfqueryparam tag in a SQL WHERE clause to validate query parameters.|
Note The data validation discussed in this chapter is done by the ColdFusion Server. Validation using |
The cfparam type attribute lets you validate the type of a parameter. You can specify that the parameter type must be any of the following values:
For example, you can use the following code to validate the variable BirthDate:
<cfparam name="BirthDate" type="date">
If the variable is not in a valid date format, an error occurs and the page stops processing.
One limitation of standard HTML forms is that you cannot validate that users input the type or range of data you expect. ColdFusion enables you to do several types of data validation by adding hidden fields to forms.
The following table describes the hidden field suffixes that you can use to do validation:
|
Note Adding a validation rule to a field does not make it a required field. You need to add a separate |
The following procedure creates a simple form for entering a start date and a salary. It uses hidden fields to ensure that you enter data and that the data is in the right format.
This example illustrates another concept that might seem surprising. You can use the same CFML page as both a form page and its action page. Because the only action is to display the values of the two variables that you enter, the action is on the same page as the form.
Using a single page for both the form and action provides the opportunity to illustrate the use of the IsDefined function to check that data exists. This way, the form does not show any results until you submit the input.
<html>
<head>
<title>Simple Data Form</title>
</head>
<body>
<h2>Simple Data Form</h2>
<!--- Form part --->
<form action="datatest.cfm" method="Post">
<input type="hidden"
name="StartDate_required"
value="You must enter a start date.">
<input type="hidden"
name="StartDate_date"
value="Enter a valid date as the start date.">
<input type="hidden"
name="Salary_required"
value="You must enter a salary.">
<input type="hidden"
name="Salary_float"
value="The salary must be a number.">
Start Date:
<input type="text"
name="StartDate" size="16"
maxlength="16"><br>
Salary:
<input type="text"
name="Salary"
size="10"
maxlength="10"><br>
<input type="reset"
name="ResetForm"
value="Clear Form">
<input type="submit"
name="SubmitForm"
value="Insert Data">
</form>
<br>
<!--- Action part --->
<cfif isdefined("Form.StartDate")>
<cfoutput>
Start Date is: #DateFormat(Form.StartDate)#<br>
Salary is: #DollarFormat(Form.Salary)#
</cfoutput>
</cfif>
</html>
When the user submits the form, ColdFusion scans the form fields to find any validation rules you specified. The rules are then used to analyze the user's input. If any of the input rules are violated, ColdFusion sends an error message to the user that explains the problem. The user then must go back to the form, correct the problem. and resubmit the form. ColdFusion does not accept form submission until the user enters the entire form correctly.
Because numeric values often contain commas and dollar signs, these characters are automatically deleted from fields with _integer, _float, or _range rules before they are validated and saved to a database.
The following table describes the code and its function:
You can use the cfqueryparam tag to validate SQL query parameters. This tag can validate the value of the SQL query parameter against a SQL data type such as REAL, TIME, or DATE. The cfqueryparam tag validates the data as follows:
The cfqueryparam tag can also validate parameter value length and its number of decimal places.
|
Note The |
The cfqueryparam tag can have any of several additional advantages, depending on the database system and Web server software that you are using:
cfqueryparam can speed database processing by using bind parameters.
The following example shows the use of cfqueryparam when valid input is given in the Course_ID variable used as a query parameter. To see what happens when you use invalid data, substitute a text string such as "test" for the integer 12 in the cfset statement.
Note that this example uses the cfsnippets database that is provided with ColdFusion, not the CompanyInfo database used in most of this book.
<html>
<head>
<title>cfqueryparam Example</title>
</head>
<body>
<h3>cfqueryparam Example</h3>
<cfset course_id=12>
<cfquery name="getFirst" datasource="cfsnippets">
SELECT *
FROM courses
WHERE Course_ID=<cfqueryparam value="#Course_ID#"
cfsqltype="CF_SQL_INTEGER">
</cfquery>
<cfoutput query="getFirst">
<p>
Course Number: #number#<br>
Description: #descript#
</p>
</cfoutput>
</body>
</html>
The following table describes the code and its function:
LiveDocs comments are not longer enabled for ColdFusion 5.0. Please use one of the following resources instead.
ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting
Version 5.0