Attribute Validation and Error Handling
In the previous section, we touched briefly on the idea of attribute
validation when we used the IsDefined( )
function
to check for the existence of an attribute before allowing processing
within the custom tag to continue. 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 ...
Get Programming ColdFusion now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.