I would highly recommend that your 'store' method does NOT through an
exception unless there is a problem. Having it check to see if 'validate'
was called is not it's job. It's job is to merely disallow the operation if
it can't proceed, and whether 'validate' was called has no bearing on that.
'validate' is a tool to help the calling code determine what an exception
means in a way that can help it be resolved.
You want me to structure my code this way (which is perfectly legit):
<cfset errors = myCFC.validate() />
<cfif structCount(errors) GT 0>
<!--- error display --->
<cfelse>
<cfset myCFC.store() />
</cfif>
But I like to do it this way, since we have an exception handling mechanism
in CF. It just reads cleaner than a bunch of CFIFs that are doing exception
handling. And in this case, it'll also save me from generating that error
struct unless I need it.
<cftry>
<cfset myCFC.store() />
<cfcatch type="IllegalStateException">
<cfset errors = myCFC.validate() />
<!--- error display --->
</cfcatch>
</cftry>
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> Behalf Of cf_nut
> Sent: Monday, October 06, 2003 10:36 AM
> To: [EMAIL PROTECTED]
> Subject: Re: [CFCDev] Data validation
>
>
> Hi Phillip,
>
> You may want to take a look at the way we've done things, briefly
> sketched here ...
>
> http://www.redev.org/archives/000035.cfm
>
> Our validate() method is not implicitly invoked by save() ( store()
> in our case ). Instead, it's the callers responsibilty to call
> validate() before calling save(). validate() returns a structure
> indicating the properties that are in error and a text message
> describing the error ( for eventual display in the presentation
> layer, typically). If either (a) the validate() method is not
> invoked, or (b) validate() indicates an error condition, then a
> subsequent save() throws an exception. It's therefore up to the
> calling code to ensure that if errors are returned by validate(),
> that suitable action is taken, such as displaying an error message
> to the user .
>
> Cheers, Andy
>
>
> --- "Phillip Cave" <[EMAIL PROTECTED]> wrote:
> >
> > I'm attempting to do the same thing. In this case I have 2 objects
> (Page and
> > PageData). When Page.Save() is called, it passes itself to the
> InsertPage()
> > method of the PageData object which does the actual database
> operations. I'm
> > just confused as to what all of these methods should be returning.
> If
> > Page.Save() calls PageData.Validate() before PageData.InsertPage()
> and there
> > are actual validation errors, what would PageData.Validate()
> return to
> > Page.Save() (I would assume either an array of validation errors
> or some
> > sort of message object)... Furthermore, what in turn does Page.Save
> () return
> > to the calling program (assuming the object was not saved because
> there were
> > validation errors)?
> >
> > Thanks,
> > Phillip
> >
> > -----Original Message-----
> > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
> > Of Justin Balog
> > Sent: Friday, October 03, 2003 9:25 AM
> > To: '[EMAIL PROTECTED]'
> > Subject: RE: [CFCDev] Data validation
> >
> >
> >
> > Just to throw in my very humble 2 cents, I do not have save() as a
> method of
> > any of my objects. I don't believe an object should be able to
> persist
> > itself. (Kind of like the terminator). I create persister objects
> that
> > handle saving to the dB. These persister objects take the type of
> object to
> > persist with a setObjToPerisit() getObjectToPersist(). That way
> if my data
> > platform changes, I don't have to revisit the objects themselves,
> I just
> > have to write a new persister method for that type of object. In
> my
> > application each object has a corresponding persistor type object
> that has a
> > bunch of persist methods saveSQLServer, saveACCESS, saveWDDX,
> saveCSV,
> > saveORACLE, etc. Just a thought.
> >
> > Take care,
> >
> > Justin
> >
> > -----Original Message-----
> > From: Phillip Cave [mailto:[EMAIL PROTECTED]
> > Sent: Friday, October 03, 2003 7:51 AM
> > To: [EMAIL PROTECTED]
> > Subject: [CFCDev] Data validation
> >
> >
> >
> > Where is the best place to handle data validation in an object?
> Should an
> > object's Save() method be responsible for validating it's internal
> members
> > before actually saving or should the calling program call a
> Validate()
> > method before attempting to save?
> >
> > Any advice is appreciated.
> >
> > Thanks,
> > Phillip
> >
> > --
> >
> > Phillip Cave
> > Director, ColdFusion Development
> > Macromedia Certified ColdFusion Developer Carol/Trevelyan Strategy
> Group
> > 202.448.5212
> >
> > ----------------------------------------------------------
> > You are subscribed to cfcdev. To unsubscribe, send an email to
> > [EMAIL PROTECTED] with the word '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]
> > ----------------------------------------------------------
> > You are subscribed to cfcdev. To unsubscribe, send an email to
> > [EMAIL PROTECTED] with the word '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]
> >
> > ----------------------------------------------------------
> > You are subscribed to cfcdev. To unsubscribe, send an email
> > to [EMAIL PROTECTED] with the word '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]
>
> ----------------------------------------------------------
> You are subscribed to cfcdev. To unsubscribe, send an email
> to [EMAIL PROTECTED] with the word '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]
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the word '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]