So this makes no sense to me at all, and it might be so terribly rare that you will never see it happen. If you use CFFORM and CFINPUT and you have two form fields of almost the same name (tack “_time” on to one of the form fields’ name) the internal coldfusion server side validation will null out the value of the form field without “_time” in its name. Whew, that was a mouthful, let me try to explain with an example. Take the following example code:
Date: <cfinput type=”text” name=”start_outage” validate=”date” required=”yes” value=”4/30/2008″><br />
Time: <cfinput type=”text” name=”start_outage_time” validate=”time” required=”yes” value=”11:54 PM”><br />
If you submit a form with these two fields, you will end up with the following output for your date field :
1899-12-30
Where the heck did that come from? Well, as it turns out, if you try to perform date functions, like DateFormat() on a zero value, it will produce the date 1899-12-30 which is I think Coldfusion’s answer to the UNIX Epoch. Anyway, I’ve got bad data, but I’m keying good data. Finally, I find a post in another discussion that indicates there is some built in server side validation Coldfusion will perform if it comes across a form field that ends with “_time”. It just so happens that a side effect is zeroing out the value of a date field that has the same form field name without the “_time” at the end. In this case the names were:
start_outage for the DATE
start_outage_time for the TIME
As soon as I changed the name of my date field to “start_outages” everthing worked. Also, changing the time field name to “start_outagetime” fixed performance and I had no more zeroed out dates. Even stranger that made debugging this problem hard was that the debug output at the bottom of my web page plainly displayed the values I expected for all of my form fields, however, as soon as I tried to output the form variables, all the dates that had matching time form fields were all zeroed out and showing “1899-12-30″.
Whew! It’s all over now and hopefully this information will help someone else out there. Here is a complete listing of code that will reproduce the problem, just paste into a single CFML template and run it:
<cfform action=”#script_name#” method=”post”>
Date: <cfinput type=”text” name=”start_outage” validate=”date” required=”yes” value=”4/30/2008″><br />
Time: <cfinput type=”text” name=”start_outage_time” validate=”time” required=”yes” value=”11:54 PM”><br />
<input type=”hidden” name=”formAction” value=”submit” />
<input type=”submit” value=”submit” />
</cfform>
<cfif formAction eq “submit”>
<cfoutput>
MySQL Date/Timestamp = #DateFormat(form.start_outage,”yyyy-mm-dd”)# #TimeFormat(form.start_outage_time,”hh:mm tt”)#
</cfoutput>
</cfif>