One of the reasons i've heard put forth to justify using getters and setters
in an object is type validation. So for instance, let's say we've got a
setter for dateOfBirth and we type the argument to be "date" so that only
valid dates can be passed into the setter.
<cffunction name="setDateOfBirth" access="private" returntype="VOID"
output="false">
<cfargument name="dateOfBirth" type="date" required="true" />
<cfset variables.instance.dateOfBirth = arguments.dateOfBirth />
</cffunction>
So far, so good.
Now what would be best practice to handle the case when the incoming
argument isn't a valid date? To those in the know, excuse my trial and error
here, but i've not attempted anything in this direction.
Attempt 1:
<cffunction name="setDateOfBirth" access="private" returntype="VOID"
output="false">
<cftry>
<cfargument name="dateOfBirth" type="date" required="true" />
<cfset variables.instance.dateOfBirth = arguments.dateOfBirth />
<cfcatch type="Any">
<cfset variables.invalidFields =
listAppend(variables.invalidFields,"dateOfBirth") />
</cfcatch>
</cftry>
</cffunction>
Result - can't instantiate the object because of the <cftry> tag at the top
of the function - "Context validation error for tag cfargument. The tag
must be nested inside a CFFUNCTION tag."
Attempt 2:
<cftry>
<cffunction name="setDateOfBirth" access="private" returntype="VOID"
output="false">
<cfargument name="dateOfBirth" type="date" required="true" />
<cfset variables.instance.dateOfBirth = arguments.dateOfBirth />
</cffunction>
<cfcatch type="Any">
<cfset variables.invalidFields =
listAppend(variables.invalidFields,"dateOfBirth") />
</cfcatch>
</cftry>
Result - the try - catch block is ignored, the error isn't handled, and we
get an error screen announcing that "The argument DATEOFBIRTH passed to
function setDateOfBirth() is not of type date."
Anyone have a suggestion how to handle type validation situations like this
seamlessly? My workaround is just to set the argument type to string, let
the invalid date in, and then deal with it in the validate method.
Thanks,
Nando
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev'
in the message of the email.
CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).
An archive of the CFCDev list is available at www.mail-archive.com/[EMAIL PROTECTED]