SebTags 1.0 Documentation: Validations

Validations

Several validation options are available in cf_sebForm.

Required

The most basic type of validation offered in cf_sebForm is simply checking for required fields. To do this, simply add a required attribute to the field:

<cf_sebField name="fName" label="First Name" required="true">

This will add both a client-side check with JavaScript as well as server-side validation.

Length

The length of a field value can be limited by using the "Length" attribute. Instead of creating a validation test, this will limit the text that can be entered. It will also truncate the value if it happens to receive a form post with a longer value.

<cf_sebField name="fName" label="First Name" Length="20">

Regular Expressions

A regular expression can be used to validate a field using the "regex" attribute. This will check the field value (if one exists) against the regular expression both on the client (with JavaScript) and on the server.

<cf_sebField name="ThreeJs" label="Enter Three Js" regex="j{3}">

Validation Types

You can use the "type" attribute, to define a data type for the field. For example, you could define a field as an integer:

<cf_sebField type="integer" name="age" label="Age">

Or as an email address:

<cf_sebField type="email" name="email" label="Email Address">

These are both validation types. Each validation type is a shortcut for a predefined regular expression.

cf_sebForm has four built-in validation types:

Custom Validation Types

You can also add your own custom validation types to cf_sebForm. To add validations to cf_sebForm, pass a structure of validations to the "validations" attribute of cf_sebForm. For example:

<cfset sValidations = StructNew()>
<cfset sValidations["YahooMail"] = "^['_a-z0-9-]+(\.['_a-z0-9-]+)*@yahoo.com">

<cf_sebForm validations="#sValidations#">
   <cf_sebField type="text" name="fName" label="First Name" required="true">
   <cf_sebField type="text" name="lName" label="Last Name">
   <cf_sebField type="YahooMail" name="email" label="Yahoo! Email Address">
   <cf_sebField type="submit" label="Save">
</cf_sebForm>

Catching Validation Errors

Sometimes, however, the validation logic may be more more complicated than a regular expression can handle. It may have to do with logical operations that can only be performed on the server.

Here is how I might write the method in the CFC:

<cffunction name="saveApplication" access="public" output="no">
	<cfargument name="fName" type="string" required="no">
	<cfargument name="lName" type="string" required="no">
	<cfargument name="email" type="string" required="no">
	
	<cfif StructKeyExists(arguments,"email") AND Len(arguments.email)>
		<cfif NOT (
				isEmail(arguments.email)
			AND	ListLast(arguments.email,"@") EQ "yahoo.com"
		)>
			<cfthrow
				message="Email address is not a legitimate working email address."
				type="Applications"
				errorcode="NotWorkingEmail"
			>
		</cfif>
	</cfif>
	
	<!--- More logic here --->

</cffunction>

While it is appropriate for .cfm to report errors to a user by writing HTML to the page, it is not appropriate for a CFC method to do so. A CFC method should return an exception to the developer via CFTHROW.

For the sake of example, I will say that this CFC is stored in Application.EmploymentApp.

Here is how this form would be set up to pass the form fields to this method on submit:

<cf_sebForm
   CFC_Component="#Application.EmploymentApp#"
   CFC_Method="saveApplication">
   <cf_sebField type="text" name="fName" label="First Name" required="true">
   <cf_sebField type="text" name="lName" label="Last Name">
   <cf_sebField type="YahooMail" name="email" label="Yahoo! Email Address">
   <cf_sebField type="submit" label="Save">
</cf_sebForm>

This would pass each form field into the "saveApplication" method of the Application.EmploymentApp component. Unfortunately, the exception would still be returned to the user as an error. Fortunately, one attribute with cf_sebForm will allow it to catch the exception as though it were a validation error:

<cf_sebForm
   CFC_Component="#Application.EmploymentApp#"
   CFC_Method="saveApplication"
   CatchErrTypes="Applications">
   <cf_sebField type="text" name="fName" label="First Name" required="true">
   <cf_sebField type="text" name="lName" label="Last Name">
   <cf_sebField type="text" name="email" label="Yahoo! Email Address">
   <cf_sebField type="submit" label="Save">
</cf_sebForm>

See it in action.

The "CatchErrTypes" attribute can hold a comma-delimited list of exception types. If the method to which cf_sebForm submits returns an error of a type in that list, cf_sebForm will return the form to the user and display the error just as it would any other validation error.