Good morning Summer, I apologize if this is a bit rambly, I've only
had a few sips of my coffee... While there are many ways to approach
validation of form field elements, I think one of the best ways is to
use a bit of a hybrid approach with both a plugin and a filter.
What I typically like to do is create a 'message' bean which
essentially stores a structure of messages which I like to key to
particular form elements. For instance, if you were wanting to create
a message or error for a particular form field, say 'Username', then
you could do something like StructInsert(variables.messages,
'username', 'The username was invalid.') within your bean. So at the
plugin-level (which is fired on every event) I would generate a fresh
message bean and store it in the request scope.
Then when you are validating your form fields (within the event that
you're posting to) you would utilize a filter to check each individual
form field for correctness and insert the errors into the message bean
if there are any. At the end of your filter, you could check to see
if there are any values stored within the message bean and figure out
what to do next. The beauty of using a filter at this point is the
fact that you can change the page flow easily using announceEvent
(like you alluded to, and make sure you pass your eventArgs as well).
The cool part about storing your errors or messages in a bean that is
keyed to the form field names is that when you redisplay your view to
show the errors to the users, you can actually provide the error
message at a per-form element basis, increasing usability of your
forms!
Adrian.
On Oct 4, 1:44 pm, "Summer S. Wilson" <collecton...@eclectic-
world.com> wrote:
> Thanks to the help earlier, I have almost the entire front-end of the
> application I'm
> working on up and running. However, I'm now working on ensuring it can handle
> bad data,
> such as folks playing with the URL or accessing an old URL for a category
> that may not
> exist anymore for the standard drill down list. In normal ColdFusion, the
> final line of
> "defense" is a try/catch around an invoke, in case of data type mismatches.
> If it fails,
> it would redirect to another page or give an error message, depending on the
> page.
>
> With the Mach-II version of the application, if there is invalid data, such
> as a
> non-numeric ID or non-existent ID being passed, how to I look for and handle
> this and
> where? In the listener, the gateway, the DAO? I tried using announceEvent,
> but that caused
> errors with the return type not being of the right type. Would it be better
> to not return
> the bean directly but put it in a structure with a result code so its always
> the right
> type, or am I missing something else? I already have the DAO set to return
> true or false
> if the bean is populated instead of the bean itself. Really need to figure
> this out as we
> are required to have all of our apps deal with this without giving any
> informative error
> messages before they can go production.
>
> For an example of one place I'm trying to do this, is the CategoryDetails
> page that loads
> when you click a category name.
>
> ~~~Snippet from the Mach-II XML~~~
> <event-handler event="CategoryDetails" access="public">
> <notify listener="categoriesListener" method="getCategoryDetail"
> resultArg="categoryBean"
> />
> <notify listener="factsheetsListener" method="getSheetsInCat"
> resultArg="qSheetsByCategory" />
> <view-page name="header" />
> <view-page name="categorydisplay" />
> <view-page name="footer" />
> </event-handler>
>
> ~~~~categoriesListener.cfc~~~~
> <cfcomponent name="CategoryListener" displayname="CategoryListener"
> output="false"
> extends="MachII.framework.Listener">
> <!--- Configures this listener as part of the Mach-II framework --->
> <cffunction name="configure" access="public" output="false"
> returntype="void">
> <!--- one time deal with our DAO and Gateway --->
> <cfset myDSN = getProperty("dsn") />
> <cfset variables.categoryDAO = createObject("component",
> "CategoryDAO").init( DSN =
> myDSN ) />
> <cfset variables.categoriesGateway = createObject("component",
> "categoriesGateway").init( DSN = myDSN ) />
> </cffunction>
>
> <!--- returns the query recordset returned by the Category Gateway --->
> <cffunction name="getCategoryList" access="public" output="false"
> returntype="query">
> <cfreturn variables.categoriesGateway.getAllCategories() />
> </cffunction>
>
> <!--- Gets detail on a single Category --->
> <cffunction name="getCategoryDetail" access="public" output="false"
> returntype="Category">
> <cfargument name="event" type="MachII.framework.Event" required="true" />
>
> <cfset var category = createObject("component", "Category").init(
> CategoryID =
> arguments.event.getArg("whichcategory") ) />
> <cfset variables.categoryDAO.read(category) />
>
> <cfreturn category />
> </cffunction>
> </cfcomponent>
>
> ~~~~Snippet from CategoryDAO.cfc~~~~
> <cffunction name="read" access="public" output="false" returntype="boolean">
> <!--- the bean to populate? --->
> <cfargument name="Category" type="Category" required="true" />
>
> <cfset var queryCategories = "" />
>
> <!--- Make sure Category requested is a valid number --->
> <cfif val(arguments.Category.getCategoryID()) GT 0>
> <!--- run the query based on the ID loaded from the Listener function --->
> <cfquery name="queryCategories" datasource="#variables.DSN#">
> SELECT categoryid, categoryname, description, range
> FROM categories
> WHERE categoryid = <cfqueryparam
> value="#arguments.Category.getCategoryID()#"
> cfsqltype="cf_sql_integer" />
> </cfquery>
>
> <!--- re-initialize the bean with data from the query --->
> <cfset arguments.Category.init( CategoryID = queryCategories.categoryid,
> CategoryName = queryCategories.categoryname,
> Description = queryCategories.description,
> Range = queryCategories.range ) />
>
> <cfreturn true />
> </cfif>
>
> <cfreturn false />
> </cffunction>
>
> Thanks in advance,
>
> Summer S. Wilson
> Collectonian on Yahoo! Messenger ~ Facebook
> (http://www.facebook.com/Collectonian)
> An Eclectic World (http://eclectic-world.com) ~ LiveJournal
> (http://collectonian.livejournal.com)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to Mach-II for CFML list.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/mach-ii-for-coldfusion?hl=en
SVN: http://greatbiztoolsllc.svn.cvsdude.com/mach-ii/
Wiki / Documentation / Tickets:
http://greatbiztoolsllc.trac.cvsdude.com/mach-ii/
-~----------~----~----~----~------~----~------~--~---