Locally scope your vars in your functions.
If you don't fancy lots of <cfset var = someVar = ""> at the top then do one
<cfset var LOCAL = {}> and "scope" your vars as you create them <cfloop
index="LOCAL.i">.
No need to create and then clear the structure.
<cfset form_errors = structNew()>
<cfset temp = structClear(form_errors)>
Plus as of some version of CF, you don't need to assign things like that to
a temp var. <cfset StructClear(myStructure)> is ok.
No need for the #s in the cfreturn.
If you find the logic for outputting the errors next to the form fields is
the same, get that in a function or CFC and simplify the form page.
You're passing in the FORM scope but you're not using it in the function.
Instead you access the FORM scope. Change all your references to
FORM.whatever in the function to ARGUMENTS.formScope or some such.
You have access as remote, if you're not really calling this remotely,
change it to public. If you are calling it remotely, you can't have optional
arguments, so doing the above will cause an error if you don't have
<cfargument name="formScope">.
Ermmmmmmm, that's all I can see.
Adrian
Building a database of ColdFusion errors at http://cferror.org/
-----Original Message-----
From: Rick Faircloth
Sent: 20 October 2008 19:03
To: cf-talk
Subject: How can I make this code better?
Hi, all...
Still figuring out the best way to employ CFC's into my workflow.
Now I'm working on incorporating CF validation into my code using CFC's.
I've got a working solution, but would like some suggestions on how to
improve it.
Thanks for any suggestions!
Rick
Here's a slimmed-down version with the highlights (or lowlights :o) :
products.cfm:
-----------------
<cfif isdefined('form.fieldnames')>
<cfinvoke component =
"imprint-warehouse-sm.components.products"
method =
"validate_product_insert_data"
returnVariable = "form_errors"
argumentCollection = "#form#" />
</cfif>
<form action="products.cfm" method="post">
Product Name <cfif isdefined('form_errors.name')>
Please enter a name for this product.</cfif><br>
<input name = "name" type="text"
value="" size="50" > <br>
Description <cfif isdefined('form_errors.name')>
Please enter a description for this product.</cfif><br>
<input name = "description" type="text"
value="" size="50" >
<input name = "submit" type="submit"
value="Submit">
</form>
products.cfc
----------------
<cffunction name = "validate_product_insert_data"
displayname = "validate_product_insert_data"
hint = "Validates form data
before product insert"
output = "false"
access = "remote">
<cfset form_errors = structNew()>
<cfset temp = structClear(form_errors)>
<cfif Not Len(Trim(form.name))>
<cfset form_errors.name = "Please enter a name for
this product.">
</cfif>
<cfif Not Len(Trim(form.description))>
<cfset form_errors.description = "Please enter a
description of this product.">
</cfif>
<cfreturn #form_errors# />
</cffunction>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Adobe® ColdFusion® 8 software 8 is the most important and dramatic release to
date
Get the Free Trial
http://ad.doubleclick.net/clk;207172674;29440083;f
Archive:
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:314155
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe:
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4