Skip to main content
July 1, 2010
Answered

use createUUID() as a form variable

  • July 1, 2010
  • 4 replies
  • 983 views

I first create a uuid using CreateUUID().

<cfset genID = createUUID()>

Following I dynamically create a form checkbox with the CreateUUID() result as name as following:

<cfinput type="checkbox" name="A#genID#">

So far, so good.

When I jump to the action page and try to check if the chechbox is checked I get a syntax error for the chexbox name.

<!--- COMPLETE CODE --->

<cfset myvar = "#createUUID()#">
<cfform>
<cfinput type="text" name="#myvar#" value="#myvar#">
  <cfinput type="submit" name="btn" value="OK">
  <hr>
  <cfif isDefined('form.#myvar#')>
  OK
</cfif>
</cfform>

<!--- ERROR --->

Parameter 1 of function IsDefined, which is now form.8E1C3559-E3D0-6094-B1FBD9B6B1DBE493, must be a syntactically valid variable name.
The error occurred on line 6.

Any solution anyone?

This topic has been closed for replies.
Correct answer Adam Cameron.

Never use isDefined().  Always use structKeyExists(). isDefined() bites.

--

Adam

PS: "never say "never"", sure, but it's sound advice 99% of the time, in this case.

4 replies

July 2, 2010

All thanks for the replies.

I have fixed my problem using 'structKeyExists' instead of 'isDefined'.

Next thing is to google for the difference between both, but that's the funny part

The other two answers are also something to keep in mind.

Regards

Inspiring
July 2, 2010

Historically, CF had rules around variable naming, eg:

* must start with a letter, underscore or currency symbol

* must contain only letters, numbers, underscores or currency symbols

This restriction has been mostly lifted, except a few situations:

* using dot-notation when referencing variables (as opposed to bracket notation);

* isDefined()

* <cfquery> (and poss some other tags) variable names

isDefined() also does a scope-hunt when looking for variables, whereas structKeyExists() just does what it's told.  So the latter is more efficient and returns more predictable results.

--

Adam

Inspiring
July 1, 2010

The problem is that variable names can't contain hyphens.

I got caught by this when I was using cold fusion uuids for the primary key of a table.  In my case, I actually wanted to append the primary key values to the end of form variables, so I used replace() to strip out the hyphens.

The way you described what you are doing, you should be able to create the uuid on your action page.

Inspiring
July 1, 2010

There are multiple answers at different levels.  The answer to your immediate question is to either not use UUID or to use replace() to parse out the dashes.

The next answer needs more information from you as to why you are creating a unique ID for the formfield name.  If we know what you are really trying to do, we can give you some hints.  For example, if you are actually creating a number of similar formfields, based on data coming back from a db query, then that's not uncommon at all.  The most common way of handling that is to imbed the value of each row's key into the formfield, maybe along with the database columnname in cases where you are creating formfields for multiple columns of each row (common in a CRUD app).  Seperate those parts of the formfield with a character like an underscore, and you can parse them out on the processing page.

The more we know about what you're trying to do, the more we can help.

-reed

Adam Cameron.Correct answer
Inspiring
July 1, 2010

Never use isDefined().  Always use structKeyExists(). isDefined() bites.

--

Adam

PS: "never say "never"", sure, but it's sound advice 99% of the time, in this case.