我正在使用命名格式报告[{field-name}]为我的ColdFusion应用程序构建一个表单,当使用RoR或CFWheels时,会在后端给我一个名为report的结构,其中包含我的所有字段名称。我使用FW / 1所以我的所有表单字段都放入RC范围而不是保留在表单范围内。我知道可以将我的表单字段转换为ColdFusion结构,因为正如我所说的,CFWheels就是这样做的。我根本不知道如何让我的应用程序这样做。
以下是我所谈论的表单的一部分
<dl class="oneColumn">
<dt class="first"><label for="report[name]">Name</label></dt>
<dd><input type="text" name="report[name]" class="text" /></dd>
<dt><label for="report[description]">Description</label></dt>
<dd><textarea name="report[description]" class="textarea"></textarea></dd>
</dl>
答案 0 :(得分:4)
Adam拥有正确的上下文,但他的代码片段错误。
一个可行的功能是:
<cffunction name="$createNestedParamStruct" returntype="struct" access="public" output="false">
<cfargument name="params" type="struct" required="true" />
<cfscript>
var loc = {};
for(loc.key in arguments.params)
{
if (Find("[", loc.key) && Right(loc.key, 1) == "]")
{
// object form field
loc.name = SpanExcluding(loc.key, "[");
// we split the key into an array so the developer can have unlimited levels of params passed in
loc.nested = ListToArray(ReplaceList(loc.key, loc.name & "[,]", ""), "[", true);
if (!StructKeyExists(arguments.params, loc.name))
arguments.params[loc.name] = {};
loc.struct = arguments.params[loc.name]; // we need a reference to the struct so we can nest other structs if needed
loc.iEnd = ArrayLen(loc.nested);
for(loc.i = 1; loc.i lte loc.iEnd; loc.i++) // looping over the array allows for infinite nesting
{
loc.item = loc.nested[loc.i];
if (!StructKeyExists(loc.struct, loc.item))
loc.struct[loc.item] = {};
if (loc.i != loc.iEnd)
loc.struct = loc.struct[loc.item]; // pass the new reference (structs pass a reference instead of a copy) to the next iteration
else
loc.struct[loc.item] = arguments.params[loc.key];
}
// delete the original key so it doesn't show up in the params
StructDelete(arguments.params, loc.key, false);
}
}
</cfscript>
<cfreturn arguments.params />
</cffunction>
我在我的应用程序中测试它(在CFWheels之外)并且它完美地工作。你要做的只是传入一个结构(在我的例子中是来自FW / 1的Rc结构),它包含应该是结构的东西,但是显示为字符串,你将返回一个具有嵌套结构的结构。
示例:
<cfscript>
Struct['hello[world]'] = 1;
Struct['hello[earth]'] = 2;
myhello = $createNestedParamStruct(Struct);
/* Now myhello equals this:
myhello.hello.world = 1;
myhello.hello.eath = 2;
*/
</cfscript>
答案 1 :(得分:3)
因此,您需要做的最基本的变革形式是:
mystruct.name = form["report[name]"];
你需要做的是编写一个循环遍历表单结构并循环表单字段名称并构建这样的结构。我猜它已经写在CFWheels的某个地方(作为一个函数),你可以通过找到它并为自己拉出它来节省自己的头痛和挫折感。
我认为就是这样,但我不确定:
<!--- helper method to recursively map a structure to build mapping paths and retrieve its values so you can have your way with a deeply nested structure --->
<cffunction name="$mapStruct" returntype="void" access="public" output="false" mixin="dispatch">
<cfargument name="map" type="struct" required="true" />
<cfargument name="struct" type="struct" required="true" />
<cfargument name="path" type="string" required="false" default="" />
<cfscript>
var loc = {};
for(loc.item in arguments.struct)
{
if (IsStruct(arguments.struct[loc.item])) // go further down the rabit hole
{
$mapStruct(map=arguments.map, struct=arguments.struct[loc.item], path="#arguments.path#[#loc.item#]");
}
else // map our position and value
{
arguments.map["#arguments.path#[#loc.item#]"] = {};
arguments.map["#arguments.path#[#loc.item#]"].value = arguments.struct[loc.item];
}
}
</cfscript>
</cffunction>