O'Reilly logo

Programming ColdFusion MX, 2nd Edition by Rob Brooks-Bilson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Attribute Validation and Error Handling

In the previous section, we touched briefly on the idea of attribute validation when we used the cfparam tag to validate and set default values for a custom tag’s attributes. This section takes things a step further by demonstrating various techniques you can use to create both required and optional attributes for your tags. Additionally, we’ll look at error- and exception-handling strategies you can use to deal with problems that occur in your custom tags.

Handling Required Attributes

Depending on the type of custom tag you create, you may want or need to require certain attributes to be passed in order for the tag to do its job. Making a tag attribute required is as simple as using a cfif statement inside the custom tag to evaluate whether the tag exists. This is usually handled by the IsDefined function:

<cfif IsDefined('attributes.MyRequiredAttrib')>

If the required attribute exists, IsDefined( ) returns true, and your tag can continue processing. If, however, IsDefined( ) returns false, the required attribute hasn’t been passed to the tag. At this point, you have a few choices:

  • Assign a default value for the missing attribute and allow processing to continue. This is one of the approaches we have used in some of the examples up to this point.

  • Return a message to the browser letting the user know that a required attribute wasn’t passed, and abort all processing using the cfabort tag. This is an approach we used in some previous examples. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required