测试是否定义了动态变量
Test if a dynamic variable is defined
我的表单 (UUID) 中有一些带有动态名称的输入变量。
为避免错误,我测试变量是否已定义,但奇怪的是,函数 IsDefined return 给我一个错误(当字段未像收音机或复选框那样发送时)。
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">
我的 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">
我的 CFM 测试:我有一个包含所有 MD_FIELD_ID 的列表并遍历它们
<cfloop list="#attributes.lMetadataField#" index="MD_FIELD_ID" delimiters="," >
<cfif IsDefined("attributes.content_" & MD_FIELD_ID)>
</cfif>
Coldfusion return 当字段不在提交的表单中时,我会这样:
Parameter 1 of function IsDefined, which is now attributes.content_BB66F151-CB09-1C8C-CCF53DE1A92652FC, must be a syntactically valid variable name.
我尝试了不同的语法:
IsDefined("attributes.content_#MD_FIELD_ID#") or
attributes["content_#MD_FIELD_ID#"]
但总是同样的错误。如果该字段在提交的表单中,则可以正常工作。
我的代码有什么问题?
在作为参数提供给 isDefined
时,您不能使用连字符(以及各种其他字符,如 :
或 #
)作为结构键名称(即变量)。相反,您可以这样做:
<cfif structKeyExists(attributes, "content_" & MD_FIELD_ID)>
structKeyExists
不计算表达式,因此不受变量名解析的影响。但是,由于这个事实,您不能方便地链接 structKeyExists
。
示例:
isDefined("someStruct.parentKey.childKey")
转换为
structKeyExists(VARIABLES, "someStruct")
and structKeyExists(someStruct, "parentKey")
and structKeyExists(someStruct["parentKey"], "childKey")
注意您需要如何检查链中每个键的存在。但它允许使用任何字符作为键名。
我的表单 (UUID) 中有一些带有动态名称的输入变量。 为避免错误,我测试变量是否已定义,但奇怪的是,函数 IsDefined return 给我一个错误(当字段未像收音机或复选框那样发送时)。
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">
我的 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">
我的 CFM 测试:我有一个包含所有 MD_FIELD_ID 的列表并遍历它们
<cfloop list="#attributes.lMetadataField#" index="MD_FIELD_ID" delimiters="," >
<cfif IsDefined("attributes.content_" & MD_FIELD_ID)>
</cfif>
Coldfusion return 当字段不在提交的表单中时,我会这样:
Parameter 1 of function IsDefined, which is now attributes.content_BB66F151-CB09-1C8C-CCF53DE1A92652FC, must be a syntactically valid variable name.
我尝试了不同的语法:
IsDefined("attributes.content_#MD_FIELD_ID#") or
attributes["content_#MD_FIELD_ID#"]
但总是同样的错误。如果该字段在提交的表单中,则可以正常工作。
我的代码有什么问题?
在作为参数提供给 isDefined
时,您不能使用连字符(以及各种其他字符,如 :
或 #
)作为结构键名称(即变量)。相反,您可以这样做:
<cfif structKeyExists(attributes, "content_" & MD_FIELD_ID)>
structKeyExists
不计算表达式,因此不受变量名解析的影响。但是,由于这个事实,您不能方便地链接 structKeyExists
。
示例:
isDefined("someStruct.parentKey.childKey")
转换为
structKeyExists(VARIABLES, "someStruct")
and structKeyExists(someStruct, "parentKey")
and structKeyExists(someStruct["parentKey"], "childKey")
注意您需要如何检查链中每个键的存在。但它允许使用任何字符作为键名。