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
CFABORTtag. This ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access