Reputation: 51
I have some input variables with a dynamic name into my form (UUID). To avoid error I test if the variables are defined, but strangely the function IsDefined return me an error (when the field is not sent like a radio or check-box).
The HTML result in the form:
Yes <input type="radio"id="content_BB66F151-CB09-1C8C-CCF53DE1A92652FC"
name="content_BB66F151-CB09-1C8C-CCF53DE1A92652FC" value="1">
No <input type="radio" id="content_BB66F151-CB09-1C8C-CCF53DE1A92652FC"
name="content_BB66F151-CB09-1C8C-CCF53DE1A92652FC" value="0">
My CFM:
Yes <input type="radio" id="content_#qMD.MD_FIELD_ID#"
name="content_#qMD.MD_FIELD_ID#" value="1"> No <input type="radio" id="content_#qMD.MD_FIELD_ID#" name="content_#qMD.MD_FIELD_ID#" value="0">
My CFM test: I have a list of all MD_FIELD_ID and loop over them
<cfloop list="#attributes.lMetadataField#" index="MD_FIELD_ID" delimiters="," >
<cfif IsDefined("attributes.content_" & MD_FIELD_ID)>
</cfif>
Coldfusion return me this when the field is not in the submitted form:
Parameter 1 of function IsDefined, which is now attributes.content_BB66F151-CB09-1C8C-CCF53DE1A92652FC, must be a syntactically valid variable name.
I have tried different syntaxes:
IsDefined("attributes.content_#MD_FIELD_ID#") or
attributes["content_#MD_FIELD_ID#"]
but always the same error. If the field is in the submitted form, it works fine.
What is wrong in my code ?
Upvotes: 3
Views: 1007
Reputation: 7833
You may not use hyphens (and various other characters like :
or #
) for struct key names (i.e. variables) when providing as argument to isDefined
. Instead you can do:
<cfif structKeyExists(attributes, "content_" & MD_FIELD_ID)>
structKeyExists
doesn't evaluate the expression and thus is not subject to the variable name parsing. However, due to that fact you cannot chain structKeyExists
as conveniently.
Example:
isDefined("someStruct.parentKey.childKey")
translates to
structKeyExists(VARIABLES, "someStruct")
and structKeyExists(someStruct, "parentKey")
and structKeyExists(someStruct["parentKey"], "childKey")
Notice how you need to check existence for every single key in the chain. But it allows to use any character as key name.
Upvotes: 6