测试是否定义了动态变量

Boo*_*ard 3 forms coldfusion

我有一些带有动态名称的输入变量到我的表单(UUID)中.为了避免错误,我测试是否定义了变量,但奇怪的是函数IsDefined返回一个错误(当字段没有像收音机或复选框那样发送时).

HTML结果的形式如下:

 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">
Run Code Online (Sandbox Code Playgroud)

我的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">
Run Code Online (Sandbox Code Playgroud)

我的CFM测试:我有一个所有MD_FIELD_ID的列表并循环它们

<cfloop list="#attributes.lMetadataField#" index="MD_FIELD_ID" delimiters="," >
<cfif IsDefined("attributes.content_" & MD_FIELD_ID)>
</cfif>
Run Code Online (Sandbox Code Playgroud)

当字段不在提交的表单中时,Coldfusion会将此返回给我:

函数IsDefined的参数1,现在是attributes.content_BB66F151-CB09-1C8C-CCF53DE1A92652FC,必须是语法上有效的变量名.

我尝试过不同的语法:

 IsDefined("attributes.content_#MD_FIELD_ID#") or
attributes["content_#MD_FIELD_ID#"]
Run Code Online (Sandbox Code Playgroud)

但总是一样的错误.如果该字段在提交的表单中,则可以正常工作.

我的代码有什么问题?

Ale*_*lex 5

在提供作为参数时,您不能使用连字符(以及各种其他字符,如:#)作为结构键名称(即变量)isDefined.相反,你可以这样做:

<cfif structKeyExists(attributes, "content_" & MD_FIELD_ID)>

structKeyExists不评估表达式,因此不受变量名称解析的影响.但是,由于这个事实,你不能structKeyExists方便地链.

例:

isDefined("someStruct.parentKey.childKey")
Run Code Online (Sandbox Code Playgroud)

翻译成

structKeyExists(VARIABLES, "someStruct")
and structKeyExists(someStruct, "parentKey")
and structKeyExists(someStruct["parentKey"], "childKey")
Run Code Online (Sandbox Code Playgroud)

请注意您需要检查链中每个键的存在性.但它允许使用任何字符作为键名.