2010-04-27 22 views
3

我有一個簡單的表格ColdFusion組件: - 傳遞給函數的參數的類型是數字

form.cfm的不是: -

<cfset Registr = createObject("component", "forms.Registr") /> 
<cfset setFoo = createObject('component','forms.Registr).putNUsr(username,password,rating) /> 

    <form name="regFrm" action="#cgi.script_name#" method="post" onsubmit="submitform();" > 
     <tr><td>Username:</td> 

    <td><input type="text" name=" Username" value="#form. Username#" ></td></tr> 


     <tr><td>Password:</td> 
    <td><input class="" type="password" name="password" value="#form.password#" ></td></tr>  

     <tr><td>Rate:</td> 

       <select name="rating" > 
        <option value="" ></option> 
        <cfloop query="qGetReting"> 
         <option value="#rating_id#" <cfif form. rating eq prof_id>selected</cfif> >#rating#</option> 
        </cfloop> 
       </select> 
      </td> 
     </tr> 
    </form> 

現在有這個CFC在叫Registr.cfc具有稱爲'putNUsr'的'Registr.cfc'代碼的插入函數的「forms」文件夾如下。

<cfcomponent> 
<cffunction name="putNUsr" returntype="void" displayname="" output="no"> 
     <cfargument name="password" type="string" required="true"> 
     <cfargument name="rating" type="numeric" required="true">   
     <cfargument name="username" type="string" required="true">  
      <cfquery datasource="#application.xyz#" name="q_putNUsr"> 
       insert into 
      users (username 
       , password 
       , rating) 

       values(    

      <cfqueryparam value="#arguments. username#" cfsqltype="CF_SQL_VARCHAR" />,   
      <cfqueryparam value="#arguments.password#" cfsqltype="CF_SQL_VARCHAR" />, 
      <cfqueryparam value="#arguments.rating#" cfsqltype="CF_SQL_INTEGER" ) 

        </cfquery> 
</cffunction> 
</cfcomponent> 

我能夠填充數據庫與數據,如果我不使用窗體域「評級」,這 是數字。 別人,我得到的錯誤如下: -

The RATING argument passed to the putNUsr function is not of type numeric. 
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. 

親切 HELP -S瓦利

回答

1

默認(第一個)選擇您的等級項目選擇傳遞一個空字符串到putNUsr組件。一個空字符串不是數字,因此無法進行類型驗證。

您必須在putNUsr組件中添加一些額外的邏輯來解決該問題,或者只需將表單中的默認評分選項設置爲0(作爲示例)即可。

4

您已將函數中參數的順序定義爲密碼,評分,用戶名,但是您將其按用戶名,密碼,評分順序傳入。

您可以選擇使用ColdFusion作爲參數傳遞參數,例如putNUsr(username ='me',password ='letmein',rating = 5)。當你這樣做時,你可以按任意順序傳遞它們

+0

也可以這樣做: rip747 2010-04-27 12:41:59

2

加里已經找到了你的bug。關於額定值的另一件事是你可以將它包裝在Val()函數中,它將給你一個空字符串0 ...

0

我在代碼中發現了同樣的問題。當我用'PasswordVal'等其他名稱替換參數'密碼'時,它就解決了。出於某種原因,Coldfusion不會讀取名爲'Password'的參數並拋出錯誤。如果您通過其他名稱更改密碼的名稱,則代碼將開始工作。

+0

如果是這樣的情況下,(I沒有測試它),其是一個不同的問題。原來的問題是由於傳遞錯誤順序的參數,即傳遞一個字符串,而不是預期的數字。 – Leigh 2013-12-31 20:48:58

相關問題