Mathew asks:
Mathew is right. I haven't played much at all with some of the newer form controls, but let's take a quick look at what he is seeing. First, the code:i know you havent used cfform much but im attempting to use the datefield in 8, to display an inline datepicker etc. the datepicker works fine on first load, but then when I submit the , i get an error saying 1-Jul-2008 is an invalid date or time string.
<cfparam name="form.etic" default="#now()#">
<cfform action="#cgi.SCRIPT_NAME#" preservedata="yes" method="post">
<cfinput type="dateField" name="etic" mask="DD-MMM-YY" style="font-size:12px;text-align:center" width="75" >
<input type="submit" name="btnSubmit" value="PRESS ME">
</cfform>
There isn't anything too special in there. When run, you see a simple form with the datefield. You see a form field with 02-Jul-08. It doesn't matter if you change the date. As soon as you hit submit you get:
02-Jul-08 is an invalid date or time string.
So what's going wrong here? As always, it helps if you examine the 'flow' of the script. We begin by defaulting form.etic to the current date and time. If you output that value you would see:
{ts '2008-07-02 08:37:22'}
We then create a form. In the cfinput/type=datefield control, we pass in the date/time and use a mask to format it.
When we submit the form, here is where things get wonky. I added a cfdump on form to help figure this out. On submitting the form, the value of the form.etic is now:
02-Jul-08
When ColdFusion tries to treat this as a date for the datefield, it barfs. Why? I don't know. This is especially odd when you consider the solution. I changed his cfparam to this:
<cfif not structKeyExists(form, "etic")>
<cfset form.etic = now()>
<cfelse>
<cfset form.etic = parseDateTime(form.etic)>
</cfif>
ColdFusion has no problems parsing the masked date. This returns a value that works just fine within the datefield. Something to watch out. I'd also assume that you would want to use this before saving the data - although I bet a cfqueryparam with the sql type set to date would handle it fine. (If not, we know what function to use!)
Archived Comments
I have found that the mask doesn't work very well except in the Flash forms and even then it is tricky. I set the value of the date input DateFormat(Now(),"mm/dd/yyyy") and it works beautifully. My CFC knows it is a date.
Be careful if you're doing and insert and the datefield on the form is optional. Handling of this can vary from one RDBMS to another. With a CF/MySQL setup you can handle this by checking the submitted form's datefield value and then controlling what to send to your SQL statement.
An example would be...
<cfif (FORM.myDateField NEQ "")>
<cfset datefieldVariable = DateFormat(FORM.myDateField,"MM/DD/YYYY")>
<cfelse>
<cfset datefieldVariable = "null">
</cfif>
Then in your SQL statement just insert the value of datefieldVariable. I can't remember if I had to do anything like this in SQL Server - I probably didn't and was just able to pass the blank string from the form.
Andy Sandefer
I'd probably do that all in the cfqueryparam:
<cfif form.mydatefield neq "">
<cfqueryparam cfsqltype="cf_sql_date" value="#dateformat(etc)#">
<cfelse>
<cfqueryparam cfsqltype="cf_sql_date" null="true">
</cfif>
My bad, I should've just told you to use
<cfif>
<cfset datefieldVariable = CreateODBCDate(FORM.myDateField)>
<cfelse>
<cfset datefieldVariable = "null">
</cfif>
Yeah - he's right - just use the queryparam
best way to get around this is to use something like CONVERT(nvarchar(10), MP.master_page_expire_date, 3) in your db query to only pull out the bits of the date you need. My problem was the db : when I queried it the date was being returned in this format 10/12/2009 12:00:00:000
I'm not certain that this is the same problem, however, when the cfinput/datefield value is passed (thru a CFAJAXPROXY call) to a JavaScript function, the creation of a new date object fails. The cfinput datefield is masked as DD-MMM-YYYY so it's sending the month text equivalent to the script. Is the best way to use JavaScript date functions to rewrite the string value back into a numeric format? Is there an easier way to manage this using built-in CF functionality? Thanks...
Not sure - do you have an example set up I could run locally?
Hi Ray, we have been using date mask in form on CF7 like:
<cfinput type="text" name="test" validate="eurodate" mask="99/99/9999" .../>
Then we upgraded to CF9, and the mask stopped working! I have tried a few things but cannot see why it should not and could not see anything from the adobe site or anywhere else...do you have any clues?
Many thanks
This worked for me, CF9:
<cfform name="foo">
<cfinput type="text" name="test" validate="eurodate" mask="99/99/9999" validateat="onblur" />
<input type="submit">
</cfform>
not luck...must something in our app/server
thanks
Thanks, just ran into this issue '... is an invalid date or time string' using a cfinput type 'datefield'.