12 September 2007
Snippet: Outputting Reactor Validation Structures
Categories: Frameworks goog ColdFusion code snippets Model-Glue
Reactor's built-in validation produces a validationError structure when it encounters something amiss, and passes it back to your viewstate. That structure is actually a structure of arrays.
I DO use Reactor's validation, and found myself copying and pasting the same snippet for outputting the validation error results a time or two this morning... figured it might come in handy for other folks, too.
A couple of things to remember:
1. The "[name of error structure here]" value is determined by the value you gave it in your event
2. When calling the viewstate.getValue() method, you can specify a default value to use if the item isn't found. In my snippet, I specified that "if the validation structure isn't there, just give me back an empty structure so my code won't break".
That's it!
I DO use Reactor's validation, and found myself copying and pasting the same snippet for outputting the validation error results a time or two this morning... figured it might come in handy for other folks, too.
code from my view template...
<cfset validation = viewstate.getValue("[name of error structure here]", structnew()) />
<cfif structcount(validation) gt 0>
<cfoutput>
<cfloop collection="#validation#" item="v">
<cfloop from="1" to="#arraylen(validation[v])#" index="i">
#validation[v][i]#<br>
</cfloop>
</cfloop>
</cfoutput>
</cfif>
<cfif structcount(validation) gt 0>
<cfoutput>
<cfloop collection="#validation#" item="v">
<cfloop from="1" to="#arraylen(validation[v])#" index="i">
#validation[v][i]#<br>
</cfloop>
</cfloop>
</cfoutput>
</cfif>
A couple of things to remember:
1. The "[name of error structure here]" value is determined by the value you gave it in your event
sample generic Commit showing where the name of the validation error structure is designated...
<message name="ModelGlue.genericCommit">
<argument name="recordName" value="ClientRecord" />
<argument name="criteria" value="ClientID" />
<argument name="object" value="Client" />
<argument name="validationName" value="PreferenceValidation" />
</message>
<argument name="recordName" value="ClientRecord" />
<argument name="criteria" value="ClientID" />
<argument name="object" value="Client" />
<argument name="validationName" value="PreferenceValidation" />
</message>
2. When calling the viewstate.getValue() method, you can specify a default value to use if the item isn't found. In my snippet, I specified that "if the validation structure isn't there, just give me back an empty structure so my code won't break".
That's it!
Posted by dougboude at 1:23 PM | PRINT THIS POST! |Link | 0 comments
Subscription Options
You are not logged in, so your subscription status for this entry is unknown. You can login or register here.
Comments
No comments found.
Post a Comment
Name: | Required | |
Email: | Required your email address will not be publicly displayed. |
Want to receive notifications when new comments are added? Login/Register for an account.
Time to take the Turing Test!!!
Sixteen plus Thirteen equalsType in the answer to the question you see above:
Your comment:
Sorry, no HTML allowed!