The final element of a modern form is adaptability. Adaptability can be defined by two questions: How hard is it to modify? How reusable are the components? Modification means that a form will continue to do its job even as conditions change. Reusability means that parts of a form can be adapted to perform other functions as they come up. Of course, we've divided our form into several working parts. It's fairly modular. But looking at what we've made so far, there is plenty of room for improvement.
For instance, if you examine all of the queries we are using, you will notice that we have hard coded our data-source name. This is a bad practice, because if the data source changes, the form will break. In order to overcome this problem, we need to change the data-source name to a variable and then include the variable in the application.cfm file. I chose #webmonkey# as my DSN variable and used this line to define it:
<cfset webmonkey = "wired_cf">
Now if the data source changes, the form can adapt quickly. Therefore, this would fall under the modification aspect of adaptability. For an example of reusability, let's take a look at our input.cfm file.
When I look at the code in input.cfm, the thing that stands out most is this code segment:
<CFIF listfindnocase(validation_error, "name")><FONT color="red"></CFIF><CFOUTPUT>Name:</CFOUTPUT><CFIF listfindnocase(validation_error, "name")></FONT></CFIF>
This is repeated for every input element on the page. Now whenever you see something repeated again and again, you'll know that there's an opportunity to condense your elements by reusing them. And since you should incorporate validation on every form that you make, it would make sense to have some kind of a reusable component you could just plug in.
You can do this by making a custom tag. It's really easy in ColdFusion. Just create a blank document and paste in the following code:
<cfif listfindnocase(#attributes.verr#, #attributes.item#)> <font color="red"></cfif><cfoutput>#attributes.item#: </cfoutput><cfif listfindnocase(#attributes.verr#, #attributes.item#)> </font></cfif>
Name the document vtag.cfm and save it. You have just created a custom tag that will change field names to red if they contain an error. In case you didn't know, you can call any ColdFusion file that ends in .cfm by using a tag syntax. All you have to do is put the filename in angle brackets (< >) and preface it with cf_. In our case, we would call the tag we just made by using this syntax: <cf_vtag>.
But because we want the tag to check if a name is on a list, we have to first pass the tag a few parameters. In this case, we should pass the name we are checking and the list to be checked. To do this, we add the attributes "item" and "verr" to the tag. "Item" is the name of the item that we are checking, and "verr" is the list that we are checking against. If the tag finds the item on the list, it will display the item in red. If not, it displays it normally. We now have an error-checking tag that we can reuse for all of our future forms.
Before we are finished, though, we need to perform one last chore. We have to go through input.cfm and change all of these blocks ...
<CFIF listfindnocase(validation_error, "name")><FONT color="red"></CFIF><CFOUTPUT>Name:</CFOUTPUT><CFIF listfindnocase(validation_error, "name")></FONT></CFIF>
... to these blocks:
<cf_vtag item="Name" verr="#validation_error#">
If you did that and it works, then congratulations, you are done! We can finally give away all of those damn books that have been sitting around forever. Yippee! You've worked hard, so take a breather and give yourself a pat on the back.
That's enough. Now get to work.