Author: Steven Neiland
Published:

Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

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.

<cfparam name="form.somedate" default="#now()#">

<cfform action="#cgi.script_name#" preservedata="yes" method="post">
<cfinput type="dateField" name="someDate" mask="DD-MMM-YY">
<cfinput type="submit" name="submit" value="Save">
</cfform>

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.

<cfif structKeyExists(form, "somedate")>
<cfset form.somedate = parseDateTime(form.somedate)>
<cfelse>
<cfset form.somedate = now()>
</cfif>

<cfform action="#cgi.SCRIPT_NAME#" preservedata="yes" method="post">
<cfinput type="dateField" name="someDate" mask="DD-MMM-YY">
<cfinput type="submit" name="submit" value="Save">
</cfform>

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.

Related Blog Postings

What Do You Think?

Reader Comments

Link Worx Seo's Gravatar
Link Worx Seo
Saturday, August 18, 2012 at 2:04:37 PM EDT

If you use the DatTimeFormat there would be no need to parse it. Place the DateTimeFormat in the SQL within a component. You could also use the form validation to validate the input once submitting the form, (onSubmit). Including cftry catch block will help prevent the problem you having with the form on refresh or re-submit. You could also create a string out of three separate date values. Month,Day,Year. Hope these ideals help you.

Brahmam's Gravatar
Brahmam
Saturday, January 18, 2014 at 4:07:31 AM EST

Thanks you very much. it was very helpful to me

Post a Comment

Comment Etiquette:

  • Please keep comments on-topic.
  • Please do not post unrelated questions or large chunks of code.
  • Please do not engage in flaming/abusive behaviour.
  • Comments that contain or appear to be advertisments, will not be published.
  • Comments that appear to be created for the purpose of linkbuilding to commercial sites will be removed.

We are all adults here so play nice.

*
*



Archives Blog Listing

Tag Listing

Learn CF In A Week

Treehouse

 
Fork me on GitHub