#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    12
    Rep 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!
  2. #2
  3. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,243
    Rep Power
    967
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    12
    Rep Power
    0
    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.
  6. #4
  7. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,243
    Rep Power
    967
    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>
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    12
    Rep Power
    0
    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.
  10. #6
  11. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,243
    Rep Power
    967
    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.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    12
    Rep Power
    0
    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>
  14. #8
  15. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,243
    Rep Power
    967
    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).
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    12
    Rep Power
    0
    Thank you. I think I finally got it working like this:

    <cfif isDefined("ARGUMENTS.ThisIsTheDateField")>,ThisIsTheDateField=#CreateODBCDate(ARGUMENTS.ThisIsTheDat eField)#</cfif>

IMN logo majestic logo threadwatch logo seochat tools logo