Skip to main content
Inspiring
November 8, 2006
Question

Dynamic variable names and invalid_character_err

  • November 8, 2006
  • 6 replies
  • 586 views
I'm neither an expert at structure notation nor dynamic variable naming conventions and would appreciate any help with the following. Thanks!

This code works fine.

<cfset idx="123">
<cfset form.product[idx]=StructNew()>
<cfparam name="form.product[idx].product_nm" default="Raspberry Jam">
<cfform name="data_entry" method="post" format="flash" height="525" width="675" action="formdump.cfm">
<cfformgroup type="tabnavigator" height="400" width="650">
<cfformgroup type="page" label="Product #idx#">
<cfinput name="static_form_name" type="text" label="Product" value="#form.product[idx].product_nm#" height="350" width="600" readonly="yes">
</cfformgroup>
</cfformgroup>
</cfform>

The following code results in the following error: "ORG.W3C.DOM.DOMEXCEPTION ERROR. Message: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified." The only change is in the "name" attribute of the <cfinput> tag:

<cfset idx="123">
<cfset form.product[idx]=StructNew()>
<cfparam name="form.product[idx].product_nm" default="Raspberry Jam">
<cfform name="data_entry" method="post" format="flash" height="525" width="675" action="formdump.cfm">
<cfformgroup type="tabnavigator" height="400" width="650">
<cfformgroup type="page" label="Product #idx#">
<!--- Change value of name attribute from "static_form_name" to "product[idx].product_nm" --->
<cfinput name="product[idx].product_nm" type="text" label="Product" value="#form.product[idx].product_nm#" height="350" width="600" readonly="yes">
</cfformgroup>
</cfformgroup>
</cfform>

Pam Grieger
plgrieger@fedex.com
This topic has been closed for replies.

6 replies

Inspiring
November 29, 2006
Ian: Sorry it's taken me so long to thank you for that last reply. Just know that your answer sent me off to a couple of very long (but productive) weeks of coding!
Inspiring
November 13, 2006
<cfif IsDefined("form['work_type_oid_nbr_'&idP]")>

Yes that would take some tweaking to get to work correctly, but since we
are treating the form as the structure it is; the struckKeyExists()
function is easier to use.

<cfif structKeyExists(form, "work_type_oid_ndb_#idP#")>
OR
<cfif structKeyExists(form, "work_type_oid_ndb_" & idP)>

Inspiring
November 13, 2006
Wow, thanks! That worked beautifully. One last question: In the following code I get an error on the IsDefined() statement. The error reads: "Parameter 1 of function IsDefined, which is now "form['work_type_oid_nbr_'&idP]", must be a syntactically valid variable name."

Could you please tell me the correct syntax?

<cfif IsDefined("form['work_type_oid_nbr_'&idP]")>
<cfselect name="work_type_oid_nbr_#idP#"
label="Work Type"
query="qMenuWorkType"
value="work_type_oid_nbr"
display="work_type_desc"
selected="#form['work_type_oid_nbr_'&idP]#">
</cfselect>
</cfif>
Inspiring
November 10, 2006
Well you can use array notation to create the dynamic name of the field,
while still assuring that the resulting field name is HTML compliant.

<input type="text" name="project_nm_#project_oid_nbr#"
value="#form['project_nm_' & project_oid_nbr]#>

The general consensus that in the latest versions of CF this array form
and the evaluate form are basically the same speed and performance wise,
the generate the same javabyte code. But most developers I know say
this from is easier to read.
Inspiring
November 10, 2006
Thanks for the info. Knowing what WON’T work is helpful!

Here’s what I’m trying to do. I’m rewriting one of my apps, switching out conventional HTML form controls for <cfform> controls in Flash format. Many of the existing forms in my app are for updating data contained in a central database. When such a form is rendered to the screen, each form control is pre-populated with existing data. All form controls are named dynamically based upon the unique ID of the record being updated. Here’s a streamlined but typical example:

<!--- User selected projects 14, 15, 16, and 17 for update. Get existing project data. --->
<cfquery name="get_project_detail" datasource="#application.DataSource#">
SELECT project_oid_nbr, project_nm
FROM project_table
WHERE project_oid_nbr IN (14,15,16,17)
ORDER BY project_oid_nbr
</cfquery>

<!--- Initialize the project_nm form control. Form names are dynamic, based upon get_project_detail.project_oid_nbr. --->
<cfloop query="get_project_detail">
<cfparam name="form.project_nm_#project_oid_nbr#" default="#get_project_detail.project_nm#">
</cfloop>

<!--- Create HTML form control. --->
<table>
<cfloop query="get_project_detail">
<tr>
<td>
<cfoutput>
Project #project_oid_nbr#:
<input type="text" name="project_nm_#project_oid_nbr#" value="#Evaluate("form.project_nm_#project_oid_nbr#")#">
</cfoutput>
</td>
</tr>
</cfloop>
</table>

This has been working just fine. However, I’m wondering if using the Evaluate() function is the most efficient way to go. Therefore I wanted to use structure notation to avoid the Evaluate() function, but as mentioned in my original post, this naming convention won’t work with <cfform> tags.

Any suggestions as to the most efficient way to get the same result while still using <cfform> tags? Thanks so much!
Inspiring
November 8, 2006
<cfinput name="product[idx].product_nm"

You can't do this. Ultimately a <cfinput tag is going to create an HTML
<input...> and the name of that input tag must follow the HTML
specifications. These specifications do not allow for any kind of
complex variable type notation.

Depending on what you are trying to do, there are some conventional work
arounds.