This content has been marked as final. Show 6 replies
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
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
WHERE project_oid_nbr IN (14,15,16,17)
ORDER BY project_oid_nbr
<!--- Initialize the project_nm form control. Form names are dynamic, based upon get_project_detail.project_oid_nbr. --->
<cfparam name="form.project_nm_#project_oid_nbr#" default="#get_project_detail.project_nm#">
<!--- Create HTML form control. --->
<input type="text" name="project_nm_#project_oid_nbr#" value="#Evaluate("form.project_nm_#project_oid_nbr#")#">
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!
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.
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?
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#")>
<cfif structKeyExists(form, "work_type_oid_ndb_" & idP)>
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!