Coldfusion: Dealing with Null values in Date Field

牧云@^-^@ 提交于 2019-12-08 05:50:34

问题


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?


回答1:


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...



来源:https://stackoverflow.com/questions/4599465/coldfusion-dealing-with-null-values-in-date-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!