Forms ColdFusion将表单值转换为结构

Forms ColdFusion将表单值转换为结构,forms,coldfusion,struct,cfwheels,fw1,Forms,Coldfusion,Struct,Cfwheels,Fw1,我正在使用命名格式报告[{field name}]为我的ColdFusion应用程序构建一个表单,当使用RoR或CFWheels时,它将在后端为我提供一个名为report的结构,其中包含我的所有字段名。我使用的是FW/1,所以我的所有表单字段都被放入RC范围,而不是留在表单范围内。我知道可以将表单字段转换为ColdFusion结构,因为正如我所说的,CFWheels可以做到这一点。我只是不知道如何让我的应用程序做到这一点 这是我所说的表单的一部分 <dl class="oneColumn"

我正在使用命名格式报告[{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>

名称
描述

因此,您需要做的最基本的更改形式是:

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>

var loc={};
for(arguments.struct中的loc.item)
{
if(IsStruct(arguments.struct[loc.item])//进一步深入拉比特洞
{
$mapStruct(map=arguments.map,struct=arguments.struct[loc.item],path=“#arguments.path#[#loc.item#]”;
}
否则//映射我们的位置和价值
{
arguments.map[“#arguments.path#[#loc.item#]]”={};
arguments.map[“#arguments.path#[#loc.item#]]”。value=arguments.struct[loc.item];
}
}

亚当的上下文是正确的,但他的代码片段是错误的

一个有效的功能是:

<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>

var loc={};
for(loc.key-in-arguments.params)
{
如果(查找(“[”,位置键)&&Right(位置键,1)=“]”)
{
//对象窗体字段
loc.name=span不包括(loc.key,“[”);
//我们将密钥拆分为一个数组,以便开发人员可以传入无限级别的参数
loc.nested=ListToArray(替换列表(loc.key,loc.name&“[,]”,“”),“[”,true);
如果(!StructKeyExists(arguments.params,loc.name))
params[loc.name]={};
loc.struct=arguments.params[loc.name];//我们需要对该结构的引用,以便在需要时可以嵌套其他结构
loc.iEnd=ArrayLen(loc.nested);
for(loc.i=1;loc.i lte loc.iEnd;loc.i++)//在阵列上循环允许无限嵌套
{
loc.item=loc.nested[loc.i];
如果(!StructKeyExists(loc.struct,loc.item))
loc.struct[loc.item]={};
如果(loc.i!=loc.iEnd)
loc.struct=loc.struct[loc.item];//将新引用(结构传递引用而不是副本)传递给下一个迭代
其他的
loc.struct[loc.item]=arguments.params[loc.key];
}
//删除原始密钥,使其不会显示在参数中
StructDelete(arguments.params,loc.key,false);
}
}
我在我的应用程序中测试了它(在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>

结构['hello[world]']=1;
结构['hello[earth]']=2;
myhello=$createNestedParamStruct(Struct);
/*现在myhello等于:
myhello.hello.world=1;
myhello.hello.eath=2;
*/

FWIW,您的表单作用域已经是一个结构。所有变量作用域都是如此。@是的,所有变量作用域都有自己的结构,但在FW/1中,我的表单和url作用域与其他一些东西混合在一起。我只需要我的表单作用域,据我所知,表单是一个无法访问的结构。正如Al所说的,表单是一个结构,您可以像y other structure.FW/1将URL和表单变量放在RC结构中,这是真的,但它不会破坏表单结构。如果绝对必要,您仍然可以使用它。谢谢Al。我没有意识到它可以从控制器访问。我将坚持使用Adam的解决方案,因为它对于任何范围都是可重用的。