0
votes

I have a question about arguments that are passed with ajax in coldfusion. So I have three arguments that I'm passing date1, date2 and meetingDate. I have to compare three of these arguments and return the string based on the dates. Here is my logic:

<cfset sigDate1 = dateFormat(trim(arguments.date1),'yyyy/mm/dd')>
<cfset sigDate2 = dateFormat(trim(arguments.date2),'yyyy/mm/dd')>
<cfset meetDate = dateFormat(URLDecode(arguments.meetingDate),'yyyy/mm/dd')>

<cfelseif (sigDate1 LT meetDate) OR (sigDate2 LT meetDate)>
    <cfset myResult = "blockDate">
<cfelse>

My current code has one problem, if I just pass one of these two dates(date1 or date2) I will always get "blockDate" returned with my ajax function. I detected the problem and if I just pass date1 but not date2 my elseif still will be executed no matter what. So I was trying to fix this problem with using one extra cfif and check if my date1 and date2 isDefined but that did not fix the problem. If anyone have any idea how to fix this bugg please let me know.

Thanks in advance.

1
I don't see a CFIF. - Evik James
Make the arguments mandatory or give them default values, whichever satisfies your business requirements. - Dan Bracuk
... Also despite the question title, the code posted uses all three (3) arguments as if they always exist. Are you actually omitting arguments when the function is called, ie yourFunctionName( date1, meetingDate) OR simply passing in an empty value yourFunctionName( date1, "", meetingDate) ? - Leigh
I'm always passing the argument no matter what. That's what cause my code to crash. So I was wondering what is the best way to prevent empty string to be compared with the date value? - espresso_coffee
Specify type="date" in your cfargument tag. - Dan Bracuk

1 Answers

1
votes

There are a couple different ways to handle this. As I understand it; the arguments always exist, but you are concerned they may have blank values.

Here are some ideas you should be able to adapt to get you started: use defaults

<cfset sigDate1 = iif(isDate(trim(arguments.date1),'dateFormat(trim(arguments.date1),'yyyy/mm/dd')','[default value]')/>
<cfset sigDate2 = iif(isDate(trim(arguments.date2),'dateFormat(trim(arguments.date2),'yyyy/mm/dd')','[default value]')/>
<cfset meetDate = iif(isDate(trim(URLDecode(arguments.meetingDate),'dateFormat(trim(URLDecode(arguments.meetingDate),'yyyy/mm/dd')','[default value]')/>

or (test dates)

<cfif isDate(trim(arguments.date1)) and isDate(trim(arguments.date2)) and isDate(URLDecode(arguments.meetingDate))>
    <cfset sigDate1 = dateFormat(trim(arguments.date1),'yyyy/mm/dd')/>
    <cfset sigDate2 = dateFormat(trim(arguments.date2),'yyyy/mm/dd')/>
    <cfset meetDate = dateFormat(URLDecode(arguments.meetingDate),'yyyy/mm/dd')/>
<cfelse>
    <--- error code --->
</cfif>

or (use try block)

<cftry>
    <cfset sigDate1 = dateFormat(trim(arguments.date1),'yyyy/mm/dd')/>
    <cfset sigDate2 = dateFormat(trim(arguments.date2),'yyyy/mm/dd')/>
    <cfset meetDate = dateFormat(URLDecode(arguments.meetingDate),'yyyy/mm/dd')/>
    <cfcatch type="expression">
        <--- error code --->
    </cfcatch>
</cftry>