Validating data in ColdFusion 9
ColdFusion 9 is brining some awesome new features and right near the top for me are implicit getters / setters. Writing these can be tedious and straight up boring but soon enough that will be a thing of the past. On top of that they have adding some validation features to these properties. Two new attributes have been added to the property tag, validate and validateparams. Rupesh Kumar has a great write up on this over on his blog but here is the short version.
Possible values of validate are.
- string
- boolean
- integer
- numeric
- date
- time
- creditcard: A 13-16 digit number conforming to the mod10 algorithm.
- email: A valid e-mail address.
- eurodate: A date-time value. Any date part must be in the format dd/mm/yy. The format can use /, -, or . characters as delimiters.
- regex: Matches input against pattern specified in validateparams.
- ssn: A U.S. social security number.
- telephone: A standard U.S. telephone number.
- UUID: A Home Universally Unique Identifier, formatted 'XXXXXXXX-XXXX-XXXX-XXXXXXXXXXXXXXX', where 'X' is a hexadecimal number.
- guid: A Universally Unique Identifier of the form "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" where 'X' is a hexadecimal number
- zipcode: U.S., 5- or 9-digit format ZIP codes
- min: Minimum value if 'validate' is integer/numeric
- max: Maximum value if the 'validate' is integer/numeric
- minLength: Minimum length of the string if the 'validate' is string
- maxLength: Maximum length of the string if the 'validate' is string
- pattern: regex expression if the validator specified in 'validate' attribute is regex
This is a quick example of a user component using some of the new validation.
This seemed like exactly what I wanted, an easy way to validate data. A closer look at how it actually works though left me wanting more. This will do exactly what it says and validate the data but this is how it's done. To show how this works we will create an instance of our user component and try to set our first name property The requirement for our first name was that it should be at least 2 characters long. I was thinking that it would somehow trap these errors but instead when I ran the example I saw the following error on screen.
The length of the string, 1 character(s), must be greater than or equal to 2 character(s).While this is nice I don't really think I could use in a real application because its not a nice way of informing the user of what went wrong. This was the base of a new project I have been working on that I hope to share with you soon.
