ColdFusion CFForm DateField Validation

Published: {ts '2011-07-12 00:00:00'}
Author: Steven Neiland
Site Url: http://www.neiland.net/article/coldfusion-cfform-datefield-validation/

As I previously blogged, ColdFusions date validation has a few quirks to watch out for. Today I encountered yet another quirk with the datefield input type.

DateField Form Fields Unable To Correctly Validate

As I continue to use the datefield type in cfforms I continue to find quirks with ColdFusion and dates. The latest one make no sense at all.

If you create a simple form with a datefield type input coldfusion can save that field correctly. However, if you reload the saved data directly back into the form coldfusion blows up when it comes to the datefield.

Fix By ReParsing The DateTime

After some googling I figured out the answer. When ColdFusion reloads the form it loads the someDate value as a string. However the datefield input type expects a datetime object and subsequently breaks.

You would think that ColdFusions DateField would be able to convert the string type to a date type automatically but no. Instead we have to code it ourselves.

What we just did was to drop the cfparam and set the form.somedate variable by either creating it for the first time or if the form was already submitted we put the datefield string through the parseDateTime function to convert it to a datetime object. For more information on this function and its partner the multi-locale LSParseDate time see the links below.