1

I've got this form with all kinds of employee certifications, I need to input a date. Sometimes this date will be months in the future other times the date will be undefined, null.

Whenever I try to pass a null value to my CFC, I always get an error that looks like:

The CPRADULTEXP argument passed to the addEmployee function is not of type date.

My Form code:

<!--- If null, set a default if not, set the default to database default --->
<cfif not isDefined("certificationsList.cprAdultExp")>
<cfinput type="datefield" required="no" name="cprAdultExp" value="" >
<cfelse>
<cfinput type="datefield" required="no" name="cprAdultExp" value="#dateformat(certificationsList.cprAdultExp, "mm/dd/yyyy")#" >
</cfif>

Form Processor:

<!--- Is the date defined? --->
<cfif len(Trim("form.cprAdultExp"))  EQ 0>
<cfinvokeargument name="cprAdultExp" value="#CreateODBCDate(Form.cprAdultExp)#">
<cfelse>
<cfinvokeargument name="cprAdultExp" value="">
</cfif>    

Right now it's passing that null value, the database is set to handle/accept nulls.

How can I fix?

Snow_Mac
  • 5,727
  • 17
  • 54
  • 80
  • 1
    An empty string "" is not a valid date. Make the cffunction argument optional, and do not pass anything if form.cprAdultExp is *not* a date. – Leigh Jan 04 '11 at 23:54
  • Is the error being thrown by CF (as you move it to the functions) or by SQL (during insert, after it has been processed by the function)? – n_kips Jan 10 '11 at 09:12

1 Answers1

3

You're leaving out the most important part - the actual CFC and the query that does the insert. What's happening is your <cfargument> tag is typed as 'date' so when you pass an empty string the validation fails. (This is one of the reasons I don't type my arguments).

You'll need to either turn off type checking or change the argument type to 'string' or 'any'. Now, when you do that you'll also need to change your <cfqueryparam> tag (you are using <cfqueryparam>, aren't you?!) to something like this:

<cfqueryparam .... null="#not len(trim(arguments.thedate))#" />

That'll fix ya...

Todd Sharp
  • 3,207
  • 2
  • 19
  • 27
  • Good answer, though I'd probably make my statement something more like 'null="#not isValid("date",arguments.theDate)#"'. That way, you're checking for date-ness and not just non-blankness. This makes the CFC more useful in a wider variety of situations. – Kyle Humfeld Jan 05 '11 at 00:55
  • 2
    That won't work if the is still typed as 'date'. The exception will still be thrown if it is passed an empty string. – Todd Sharp Jan 05 '11 at 01:26
  • 1
    I am using the cfargument but am not using any cfqueryparam, why would I want/need to use cfqueryparam? – Snow_Mac Jan 05 '11 at 15:46
  • 3
    For starters it will help prevent SQL injection attacks which have the potential to be a major security problem. They also will ultimately provide better performance through the use of bound parameters. I *strongly* suggest you research more and immediately start using it. This should get you started: http://bit.ly/g3fWH6 – Todd Sharp Jan 05 '11 at 16:05