Coldfusion:处理日期字段中的空值
我有这张包含各种员工证明的表格,我需要输入日期。有时该日期是未来几个月的日期,有时该日期是未定义的,为空。
每当我尝试将空值传递给 CFC 时,我总是会收到如下错误:
传递给 addEmployee 函数的 CPRADULTEXP 参数不是日期类型。
我的表单代码:
<!--- 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>
表单处理器:
<!--- Is the date defined? --->
<cfif len(Trim("form.cprAdultExp")) EQ 0>
<cfinvokeargument name="cprAdultExp" value="#CreateODBCDate(Form.cprAdultExp)#">
<cfelse>
<cfinvokeargument name="cprAdultExp" value="">
</cfif>
现在它正在传递该空值,数据库设置为处理/接受空值。
我该如何修复?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您遗漏了最重要的部分 - 实际的 CFC 和执行插入的查询。发生的情况是您的
标记输入为“date”,因此当您传递空字符串时验证会失败。 (这是我不输入论点的原因之一)。您需要关闭类型检查或将参数类型更改为“字符串”或“任意”。现在,当您这样做时,您还需要更改
标记(您正在使用
,是吗?!)这样的话:这会解决你的问题......
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:That'll fix ya...
对于 Lucee 类型的 CFML,设置是向后兼容的,但应该可以使其更加 OOP 以允许配置中为 null。
对于函数参数以及我们使用结构体来说,从数据库字段 null 到空字符串的转换非常烦人。
如果我们将数据插入数据库,我们必须在查询参数中使用 null=="" 。
for the Lucee kind of CFML, the setup was backwards compatibility but it should possible to make it more OOP to allow null in config.
the cast from database fields null to an empty string is very annoying, for function arguments and also if we use a struct.
and if we insert data back into database we have to use null=="" in query parameter.