Skip to main content
Known Participant
February 24, 2012
Answered

Date field (not required) causing an error...Please assist!

  • February 24, 2012
  • 5 replies
  • 4816 views

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!


    This topic has been closed for replies.
    Correct answer

    ear,

    If you are going to use an optional argument in your function, you need to be able to check whether or not a value for the argument got passed.  Use IsDefined("arguments.ThisIsTheDataField") to determine this:

    <cfif IsDefined("arguments.ThisIsTheDataField")>

       <!--- Stuff that happens if the user passed this argument --->

    <cfelse>

       <!--- Stuff that happens if the user did not pass this argument --->

    </cfif>

    It looks like you may also want to review the documentation on Coldfusion Functions here (http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7ff3.html).

    5 replies

    earwig75Author
    Known Participant
    February 27, 2012

    Thanks for the help. I with validate=date and required=no it lets the person filling out the form leave it blank... if anything is entered it validates it to make sure they enter a "good" date.

    I ended up getting this all working by using this in my CFC:

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

    I actually set up each one this way now.

    Thanks again for the help.

    
    BKBK
    Community Expert
    Community Expert
    February 27, 2012

    earwig75 wrote:

    Thanks for the help. I with validate=date and required=no it lets the person filling out the form leave it blank... if anything is entered it validates it to make sure they enter a "good" date.

    I ended up getting this all working by using this in my CFC:

    <cfif isDefined("ARGUMENTS.ThisIsTheDateField")>,ThisIsTheDateField=#Create ODBCDate(ARGUMENTS.ThisIsTheDateField)#</cfif>

    I actually set up each one this way now.

    Thanks again for the help.

    

    Good for you! Then please kindly mark the question as answered. This will help others looking for similar answers in future. Also, it will prevent someone with the intention to help, from reading the entire thread, to find out only at the end that the problem had already been solved.

    BKBK
    Community Expert
    Community Expert
    February 26, 2012

    earwig75 wrote:

       <cfinput type="Text"

                name="ThisIsTheDateField"

                value="#ThisIsTheDateField#"

                message="ThisIsTheDateField must be a valid date"

                required="no"

                validate="date"

                validateAt="onSubmit"

                size="50"

                maxlength="10">

    This tag contains a contradiction. If you insist that it be a valid date, then required must be set to Yes. Otherwise a blank field would be permissible. but then, that is not a valid date. Also the message could be simpler and more instructive, for example, "Must be a valid date, in the format mm/dd/yyyy".

    <cfargument name="ThisIsTheDateField"

                  type="date"

                  required="no"

                  hint="ThisIsTheDateField field">

    If you didn't want ColdFusion to complain you could have just used:

    <cfargument name="ThisIsTheDateField"  required="no"  hint="ThisIsTheDateField field">

    Inspiring
    February 26, 2012

    Regarding:

    <cfinput type="Text"

                name="ThisIsTheDateField"

                value="#ThisIsTheDateField#"

                message="ThisIsTheDateField must be a valid date"

                required="no"

                validate="date"

                validateAt="onSubmit"

                size="50"

                maxlength="10">

    This tag contains a contradiction. If you insist that it be a valid date, then required must be set to Yes. Otherwise a blank field would be permissible. but then, that is not a valid date.

    Actually, it meets his requirements.  The subject line of this thread states that a date is not required so a blank field is permissible.  However, if the user submits anything, it has to be a valid date.

    BKBK
    Community Expert
    Community Expert
    February 26, 2012

    Dan Bracuk wrote:

    Regarding:

    <cfinput type="Text"

                name="ThisIsTheDateField"

                value="#ThisIsTheDateField#"

                message="ThisIsTheDateField must be a valid date"

                required="no"

                validate="date"

                validateAt="onSubmit"

                size="50"

                maxlength="10">

    Actually, it meets his requirements.  The subject line of this thread states that a date is not required so a blank field is permissible.  However, if the user submits anything, it has to be a valid date.

    I am not arguing his requirements. The man pay him own money, him make him own choice. However, I am assuming that, in view of the validation, ColdFusion would forbid the submission of a blank field. I should hurry to say it's a hunch I haven't tested.

    earwig75Author
    Known Participant
    February 24, 2012

    I believe what you just showed me is what I tried... but I get the below error. I am really confused Do I have it laid out wrong still?

    Context validation error for the cfargument tag.

    The tag must be nested inside a CFFUNCTION tag.

    This is what my function looks like now...

    <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.ThisIsTheDataField")>

    <cfargument name="ThisIsTheDateField"

                  type="date"

                  required="no"

                  hint="ThisIsTheDateField field">

    <cfelse>

    </cfif>

      <cfargument name="Fourth Thing"

                  type="string"

                  required="no"

                  hint="Fourth Thing">

    February 24, 2012

    The cfif does not surround the cfargument.  Put your cfargument for ThisIsTheDateField the same way you have it for your First/Second/Other Thing cfargument.  Then, understand that even though you have a cfargument tag that is named "ThisIsTheDataField", that the variable "arguments.ThisIsTheDataField" doesn't necessarily exist (because you have defined the argument as optional).  If it doesn't, you can't use it, otherwise you will get

    Element THISISTHEDATEFIELD is undefined in ARGUMENTS.

    So for the parts of your function where you will want to do something specific if the ThisIsTheDataField was passed to your function, you have to check using IsDefined("argument.ThisIsTheDataField") to determine if that variable exists or not.  SO, to summarize using the example you supplied above:

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

    <cfif IsDefined("arguments.ThisIsTheDateField")>

       <!--- operations that happen if ThisIsTheDateField was passed to the function

    <cfelse>

        <!--- operations that happen if ThisIsTheDateField was not passed to the function --->

    </cfif>

    As an example, let us say that you want to have the date be a default value (say, today's date), if the date isn't passed to the function.  You could do:

    <cfif IsDefined("arguments.ThisIsTheDataField")>

        <cfset TheDateField = arguments.ThisIsTheDataField" />

    <cfelse>

        <cfset TheDateField = Now() />

    </cfif>

    At the end of this code, the variable "TheDateField" will be guaranteed to have a date, and the date will be the date passed to the function if one was passed, or otherwise today's date if not.

    I hope that clarifies everything!

    earwig75Author
    Known Participant
    February 24, 2012

    mw_scitent so you are saying to add the IsDefined AFTER my arguments inside the CFFunction tag like this?

    <cfargument name="Fourth Thing"

                  type="string"

                  required="no"

                  hint="Fourth Thing">

    <cfif IsDefined("arguments.ThisIsTheDateField")>

       <!--- operations that happen if ThisIsTheDateField was passed to the function

    <cfelse>

        <!--- operations that happen if ThisIsTheDateField was not passed to the function --->

    </cfif>

    <cfif IsDefined("arguments.ThisIsTheDataField")>

        <cfset TheDateField = arguments.ThisIsTheDataField" />

    <cfelse>

        <cfset TheDateField = Now() />

    </cfif>

    <cfquery datasource="#mysource#">

    INSERT INTO [DB].[dbo].MyTable(bla bla bla)

    VALUES(blablabla)

    </cfquery>

      <cfreturn true>

    </cffunction>

    Thanks

    earwig75Author
    Known Participant
    February 24, 2012

    I tried the cfif like mw_scitent showed me... but now I am getting this error:

    Element THISISTHEDATEFIELD is undefined in ARGUMENTS.

    Do I need to put some cfif in the arguments too?

    Thanks again guys.

    Correct answer
    February 24, 2012

    ear,

    If you are going to use an optional argument in your function, you need to be able to check whether or not a value for the argument got passed.  Use IsDefined("arguments.ThisIsTheDataField") to determine this:

    <cfif IsDefined("arguments.ThisIsTheDataField")>

       <!--- Stuff that happens if the user passed this argument --->

    <cfelse>

       <!--- Stuff that happens if the user did not pass this argument --->

    </cfif>

    It looks like you may also want to review the documentation on Coldfusion Functions here (http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7ff3.html).

    earwig75Author
    Known Participant
    February 24, 2012

    Thank you mw_scitent. I have spent a long time reading through Adobe's documentation and I tried a few different methods before posting here.

    I also tried adding the <cfif IsDefined("arguments.ThisIsTheDataField")> into my .cfc like this but it tells me it must be nested in a cffunction - I'd like to think there is a way to do this without building a new cffunction and query etc... am I doing this wrong still?

    Thanks again, and I'm sorry if I come across as stupid or not trying. I really have been wracking my brain on this.

    <cfif IsDefined("arguments.ThisIsTheDataField")>

    <cfargument name="ThisIsTheDateField"

                  type="date"

                  required="no"

                  hint="ThisIsTheDateField field">

    <cfelse>

    </cfif>

    February 24, 2012

    ear,

    Your method is optional.  But since you have declared it as type "date", if you do pass it a value, it must be in valid date format.  If you pass it a blank string, it will choke.

    The answer is that you have to filter out blanks prior to calling <cfinvokeargument>.  A verbose and readable solution could look like:

    <cfif Len(Form.ThisIsTheDateField) gt 0>

      <cfinvokeargument name="ThisIsTheDateField" value="#DateFormat(FORM.ThisIsTheDateField)#">

    </cfif>

    You might also consider the case where ThisIsTheDateField is not blank, but also not a valid date format.  If you need to handle that case as well, you may need to make use of the IsDate function to test it.

    Good luck!

    earwig75Author
    Known Participant
    February 24, 2012

    mw_scitent, thank you VERY much for the reply, I will give this a try.

    In response to your 2nd suggestion, I use this on the text field:

                message="ThisIsTheDateField must be a valid date"

                required="no"

                validate="date"

                validateAt="onSubmit"

    wouldn't this take care of forcing a valid date format? Thank you again.

    Inspiring
    February 24, 2012

    That will probably prevent a form submission with a string that does not represent a date.  To be sure, try it and see what happens.

    Something you should know is that cfform uses javascript.  This can be disabled by the user so it's not bulletproof.  You might want validate it on the server as well.  Two functions you can use are isValid() and isDate().