简体   繁体   中英

JQuery date type , Sybase date type sent to CF function

I have a Sybase SP which takes a date as the sole argument and returns information about an order. I get the date from a JQuery datepicker on a CF page - which is formatted to yy-mm-dd. The SP defaults to getDate() when none is entered. When I enter a date on the form everything works fine. When I do not send a date - the date field is left blank - I get a CF error telling me that the argument is not of type date. I know the SP is sending a datetime value but how can I get the CF function to recognize it?

In the SP...

declare @order_dt datetime 

If @order_dt IS NULL
BEGIN
SELECT @order_dt = getDate()
END

In the cfc...

<cfargument name="order_dt" type="date" required="true" hint="order date to search for" >

and

 <cfprocparam type="In" cfsqltype="CF_SQL_DATE" dbvarname="@order_dt" value="#order_dt#" null="No">

On the .cfm page...

<!--JQuery datepicker  -->
<script type="text/javascript">
   $(function() {
           $("#datepicker").datepicker({ dateFormat: "yy-mm-dd" }).val()
   });

And the form looks like this:

<form id="xxxxForm">
<label for="date1">Pick a Date... </label>
<br>
<p><input type="text" id="datepicker" value="" name="date1"></p>
Then choose 'Search'...<br>
<input type="submit" name="submit" value="Search">
<br>
<br>

</form>

How can I get CF to recognize this as a good value? I was thinking perhaps if I declare @order_dt as just a date??

> <cfargument name="order_dt" type="date" required="true" 

When you leave the date field blank, the submitted value is an empty string "". An empty string is not a valid date . That is why CF complains when you try and invoke the function.

As mentioned in the comments, you could use required="false" or default="#now()#" . But for it to work as you expect, you must also omit the argument when the date is left blank. If you pass anything to the function, even an empty string, the default will not kick in. But validation will. So you will still get the same error.

Another option is to change the argument type to string . Then validate inside the function. If the supplied value is not a valid date, either replace it with now()

   <cfargument name="order_dt" type="string" default="">

   <!--- simplistic date validation for illustration --->
   <cfif NOT isDate(arguments.order_dt)>
       <cfset arguments.order_dt = now()>
   </cfif>

OR just pass NULL to the stored procedure

   <cfargument name="order_dt" type="string" default="">
   ...

   <cfprocparam type="In" value="#arguments.order_dt#" 
         null="#not isDate(arguments.order_dt)#" ....>

On a side note, dbvarname was deprecated a while back. In recent versions it does nothing. Only positional parameters are supported.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM