The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> ColdFusion Development
|
Receiving error on non-required date field - please help
Discuss Receiving error on non-required date field - please help in the ColdFusion Development forum on Dev Shed. Receiving error on non-required date field - please help ColdFusion Development forum discussing CFML coding practices, tips on CFML, and other CFML related topics. Find out why ColdFusion is the tool of choice for many e-commerce developers.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 24th, 2012, 08:32 AM
|
|
Registered User
|
|
Join Date: Feb 2012
Posts: 12
Time spent in forums: 2 h 13 m 55 sec
Reputation Power: 0
|
|
|
Receiving error on non-required date field - please help
Hello, I am a beginner when it comes to ColdFusion could someone please assist with this issue?
I have a date field in my application that is not required, I want users to have the choice to leave this field blank.
Below is a breakdown of what I have going on:
On Add/Edit page:
<cfinvoke component="test"
method="get"
ReferenceNumber="#URL.RefIDNum#"
returnvariable="record">
<cfset ThisIsTheDateField=DateFormat(record.ThisIsTheDateField, "MM/DD/YYYY")>
<cfform action="process.cfm">
<cfinput type="Text"
name="ThisIsTheDateField"
value="#ThisIsTheDateField#"
message="ThisIsTheDateField must be a valid date"
required="no"
validate="date"
validateAt="onSubmit"
size="50"
maxlength="10">
On process.cfm:
<cfinvokeargument name="ThisIsTheDateField"
value="#DateFormat(FORM.ThisIsTheDateField)#">
On CFC Page:
<!--- Method arguments --->
<cfargument name="ThisIsTheDateField"
type="date"
required="no"
hint="ThisIsTheDateField field">
Query Value for this field:
#CreateODBCDate(ARGUMENTS.ThisIsTheDateField)#,
The error I get is:
Error Occurred While Processing Request
The THISISTHEDATEFIELD argument passed to the add function is not of type date.
If the component name is specified as a type of this argument, it is possible that either a definition file for the component cannot be found or is not accessible.
The error occurred in E:/site/test.cfc: line 56
54 :
55 : <!--- Add a record --->
56 : <cffunction name="add"
57 : returntype="boolean"
58 : hint="Add a record">
I need a way to make this field accept blank entries... when I leave it blank I get the above error!
Thank you!
|

February 24th, 2012, 09:15 AM
|
|
Moderator
|
|
Join Date: Jun 2002
Location: Raleigh, NC
|
|
|
The problem is you have type="date" on your cfproperty and you are passing in a value that is not a date (an empty string). So you need to use a cfif and only pass the value to the CFC as an argument if it is not an empty string.
|

February 24th, 2012, 10:11 AM
|
|
Registered User
|
|
Join Date: Feb 2012
Posts: 12
Time spent in forums: 2 h 13 m 55 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by kiteless The problem is you have type="date" on your cfproperty and you are passing in a value that is not a date (an empty string). So you need to use a cfif and only pass the value to the CFC as an argument if it is not an empty string. |
When I don't pass anything I get this error:
Element THISISTHEDATEFIELD is undefined in ARGUMENTS.
Do I need a cfif on the arguments in the cfc as well? If so, would you be able to give me an example?
Thanks again for your help.
|

February 24th, 2012, 10:55 AM
|
|
Moderator
|
|
Join Date: Jun 2002
Location: Raleigh, NC
|
|
|
If you don't pass in that argument you can't reference it within the CFC function unless you check for its existence there as well. So something like:
<cfif StructKeyExists( arguments, "thisIsTheDateField" )>
....the argument was passed
<cfelse>
...the argument was not passed
</cfif>
|

February 24th, 2012, 11:13 AM
|
|
Registered User
|
|
Join Date: Feb 2012
Posts: 12
Time spent in forums: 2 h 13 m 55 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by kiteless If you don't pass in that argument you can't reference it within the CFC function unless you check for its existence there as well. So something like:
<cfif StructKeyExists( arguments, "thisIsTheDateField" )>
....the argument was passed
<cfelse>
...the argument was not passed
</cfif> |
Thanks, I have been trying something similar to that but it keeps failing. The error says that it must be nested within a CFFuntion...which it is. Can I put this in where my other arguments are?
Thanks again.
|

February 24th, 2012, 11:22 AM
|
|
Moderator
|
|
Join Date: Jun 2002
Location: Raleigh, NC
|
|
|
No the first part of a method must be the cfarguments. You can't do anything with cfif in the cfarguments block. You have to do the check in the method body.
|

February 24th, 2012, 11:53 AM
|
|
Registered User
|
|
Join Date: Feb 2012
Posts: 12
Time spent in forums: 2 h 13 m 55 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by kiteless No the first part of a method must be the cfarguments. You can't do anything with cfif in the cfarguments block. You have to do the check in the method body. |
Not to sound stupid but, where is the method body? I have been going crazy trying to get this to work and I get errors no matter what I try. Below is an example of what I have... can you tell me what I'm doing wrong? Thanks again.
<cffunction name="add"
returntype="boolean"
hint="Add a record">
<!--- Method arguments --->
<cfargument name="FirstThing"
type="string"
required="no"
hint="First Thing">
<cfargument name="SecondThing"
type="string"
required="no"
hint="Second Thing">
<cfif IsDefined("arguments.ThisIsTheDateField")>
<cfargument name="ThisIsTheDateField"
type="date"
required="no"
hint="ThisIsTheDateField field">
<cfelse>
</cfif>
<cfargument name="Fourth Thing"
type="string"
required="no"
hint="Fourth Thing">
<cfquery datasource="#mysource#">
INSERT INTO [DB].[dbo].MyTable(bla bla bla)
VALUES(blablabla)
</cfquery>
<cfreturn true>
</cffunction>
|

February 24th, 2012, 03:01 PM
|
|
Moderator
|
|
Join Date: Jun 2002
Location: Raleigh, NC
|
|
Code:
<cffunction name="add" returntype="boolean" hint="Add a record">
<!--- Method arguments --->
<cfargument name="FirstThing" type="string" required="no" hint="First Thing">
<cfargument name="SecondThing" type="string" required="no" hint="Second Thing">
<cfargument name="ThisIsTheDateField" type="date" required="no" hint="ThisIsTheDateField field">
<cfargument name="Fourth Thing" type="string" required="no" hint="Fourth Thing">
<!--- Everything after this is the method body. --->
<cfif StructKeyExists( arguments "ThisIsTheDateField" )>
<cfquery datasource="#mysource#">
INSERT INTO [DB].[dbo].MyTable(bla bla bla)
VALUES(blablabla)
</cfquery>
<cfelse>
...Do whatever else
</cfif>
<cfreturn true>
</cffunction>
Of course that will get old fast if you have multiple optional arguments. The easier way to do this is in the cfqueryparam tags in your cfquery, check the value and insert null if it isn't there:
Code:
<cfquery>
INSERT INTO....
VALUES(
...other cfqueryparams...
<cfif StructKeyExists( arguments "ThisIsTheDateField" )>
<cfqueryparam value="#arguments.ThisIsTheDateField#" cfsqltype="CF_SQL_DATE">
<cfelse>
<cfqueryparam value="" cfsqltype="CF_SQL_DATE" null="yes" >
</cfif>
)
</cfquery>
There are ways this can be streamlined even more if you're on CF 9 (using ternary operators).
|

February 24th, 2012, 03:38 PM
|
|
Registered User
|
|
Join Date: Feb 2012
Posts: 12
Time spent in forums: 2 h 13 m 55 sec
Reputation Power: 0
|
|
|
Thank you. I think I finally got it working like this:
<cfif isDefined("ARGUMENTS.ThisIsTheDateField")>,ThisIsTheDateField=#CreateODBCDate(ARGUMENTS.ThisIsTheDateField)#</cfif>
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|