Previously in this series, we installed the ModelGlue:Unity framework and the ColdSpring framework. We used the ModelGlueApplicationTemplate as the skeleton, added our basic flow and navigation. We can save and list contacts and our validation is working nicely,
In this section of our Contact-O-Matic, we will pull out the contact types values from coldspring and used them to add defined categories to our contacts. In a previous segment, we added the Contact Types as a 'map' in ColdSpring, now we will pull out the values and add them to our form.
Inside the *ContactManagerMG/Config/ColdSpring.xml file the bean definition for ContactService.cfc looks like this:
In order to provide a proper separation in our code, we add a new 'message-listener' tag with the message attribute of 'needContactTypes' and the function attribute of 'getContactTypes'.
Then in our 'contact.view' event-handler, add a new message tag inside broadcasts tag. Your 'contact.view' event-handler should look as follows:
<message name="needContactForm" />
<message name="needContactTypes" />
<include name="body" template="frmContact.cfm">
<value name="whichMenuIsCurrent" value="contact.view" />
<result do="view.template" />
Note: If copying this code directly, you must change the & lt; to a proper <
Next, in our *ContactManagerMG.controller.controller.cfc, we add a new function called 'getContactTypes'. This function will pull out the 'ContactTypes' map, or struct, from our ContactService and place them in the event.
<cfset ContactService.saveContact( ContactFormBean ) />
<cfset arguments.event.addResult("Success") />
<cfset arguments.event.setValue("ErrorStruct", ErrorStruct) />
<cfset arguments.event.addResult("Failure") />
Finally, we open our form, located at *ContactManagerMG/views/frmContact.cfm. Pull out the ContactTypes from the event and loop through to create a select box of values. Your completed form should look like this:
<cfset ContactFormBean = viewstate.getValue("ContactFormBean") />
<cfset ErrorStruct = viewstate.getValue("ErrorStruct", structNew()) />
<cfset ContactTypes = viewstate.getValue("ContactTypes") />
<cfif structCount( ErrorStruct ) GT 0 >
<cfloop collection="#ErrorStruct#" item="errorType">
<li>#ErrorStruct[ errorType ]#</li>
<form id="ContactForm" action="#myself#SubmitContactForm" method="post">
<input type="hidden" name="contactID" value="#ContactFormBean.getContactID()#" />
Name: <input type="text" name="ContactName" value="#ContactFormBean.getContactName()#"><br />
Type: <select name="ContactType">
<cfloop collection="#ContactTypes#" item="thisType">
<option value="#thisType#" <cfif ContactFormBean.getContactType() IS thisType>selected</cfif>>#ContactTypes[thisType]#</option>
<input type="submit" name="submit" value="Submit" />
Once you reset your application and click the 'contact' tab, you should have a functional select box in your form.
Proper application design would most likely not include storing the data for your select boxes inside coldspring. After all, that is what a database is for. The purpose of this exercise is to show how to retrieve data from ColdSpring to use in your applications.
A practical use of ColdSpring would be to define an email service inside ColdSpring to operate in two modes, production and development. In production mode, the email service would send emails to each email it was passed. In development mode, the email service would send an email to a developer email address. Thus, changing from development mode to production mode is as easy as changing a line in your ColdSpring.xml file.
If you have ever accidently spammed your users whilst testing application email functionality, you can appreciate the benefits ColdSpring offers.
The fruits of our labor