Going back and updating old code sucks. Especially, if you need to update code because it was statically coded. I have been migrating a an older registration form for my current project, and noticed a the credit card year date field still had 2006 in the select list. I created a solution that was reusable for any of my projects and I would not have to worry about updating the years ever again.
The current project is being built from scratch and has allowed me to really implement some best practices to save me time and resources. I wanted a something that could be reused, so I allowed the user to change the field name of the drop list and the number of years in the future they care to display.
components.cfc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <cfcomponent> <cffunction name="getCCyearfield" output="no" returntype="string"> <cfargument name="FieldName" required="no" type="string" default="ExpirationYear" /> <cfargument name="YearsAhead" required="no" type="numeric" default="15" /> <cfset ccyearselect = ""> <cfset thisyear = val(dateformat(now(),'yyyy'))> <cfset futuredate = val(dateformat(dateadd('yyyy',ARGUMENTS.YearsAhead,now()),'yyyy'))> <cfoutput> <cfsavecontent variable="ccyearselect"> <select name="#ARGUMENTS.FieldName#"> <option value="">Choose Expiration Year</option> <cfloop from="#thisyear#" to="#futuredate#" index="i"> <option value="#i#">#i#</option> </cfloop> </select> </cfsavecontent> </cfoutput> <cfreturn ccyearselect /> </cffunction> </cfcomponent> |
The display of the field is fairly straightforward. Optional arguments are “FieldName” and “YearsAhead”.
display.cfm
1 2 3 4 5 6 | <cfinvoke component="components" method="getCCyearfield" returnvariable="CCYear"> </cfinvoke> <cfoutput>#CCYear#</cfoutput> |
The code itself is not rocket science. However, it should save me, and hopefully you, the effort of updating a static credit card year select field.
Was this helpful for you? Let me know.





December 17th, 2008 at 1:09 am
Jason,
Thanks for the post!
I actually do something very similar.
I tend to use the same kinds of fields in forms over and over, so over the years I have built a small collection of form helper functions.
The started as UDFs and custom tags and have been migrated over to a CFC collection as you have here.
I do have a generic Year function that looks similar to yours, but has a few more parameters: optional starting year which defaults to current year and the selected year which is not required.
The selected year allows me to use this function on edit forms as well.
I have similar functions for Months (with a flag for Number vs Full Name), States (flag for 2 letter abbreviation vs Full Name), along with some other generic functions that apply specifically to the business rules of my clients.
I have never looked around to see if something like this is shared anywhere, but if there is interest, I can write it all up and share it (it is all fairly simple and similar to what you have already done above).
I would also comment that if you happen to be storing Credit Card information, your drop down would need to support past years, so if my card expires Dec 2007 and I go into your form in Jan 2008 to edit the expiration dates, your drop down should actually show 2007 as currently selected.
I take from your description above that you are just talking about an input form for taking current/valid expiration dates, but I just wanted to throw that out there.
December 17th, 2008 at 1:30 am
If you’re doing credit card payments, it would be great to have you take a look at my new project cfpayment: http://cfpayment.riaforge.org. It wouldn’t directly address the UI stuff above but you might find it helpful/interesting.