One of the limitations of HTML forms is the inability to define input fields as required. Because this is a particularly important requirement for database applications, ColdFusion lets you require users to enter data in fields. To specify a field as required, you can do either of the following:
required
attribute of the cfinput
, cfselect
, cftextarea
, and cftree
tags.
name
attribute composed of the field name and the suffix "_required". You can use this technique with CFML and HTML form tags.
For example, to require that the user enter a value in the FirstName field of a cfinput
tag, use the following syntax:
<cfinput type="Text" name="FirstName" size="20" maxlength="35" required="Yes">
To require that the user enter a value in the FirstName field of an HTML input
tag, use the following syntax:
<input type="Text" name="FirstName" size="20" maxlength="35"> <input type="hidden" name="FirstName_required">
In either of these examples, if the user leaves the FirstName field empty, ColdFusion rejects the form submittal and returns a message informing the user that the field is required. You can customize the contents of this error message.
If you use a required
attribute, you customize the message by using the message
attribute, as follows:
<cfinput type="Text" name="FirstName" size="20" maxlength="35" required="Yes" message="You must enter your first name.">
If you use a hidden field tag, you customize the message using the value
attribute of the hidden field, as follows:
<input type="hidden" name="FirstName_required" value="You must enter your first name.">