ColdFusion 将表单值转换为结构

发布于 2024-10-19 12:05:33 字数 693 浏览 0 评论 0原文

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

I am building a form for my ColdFusion application using the naming format report[{field-name}] which when using RoR or CFWheels would give me a struct on the backend called report containing all of my field names. I am using FW/1 so all of my form fields get put into the RC scope rather than remaining in the Form scope. I know that it is possible to convert my form fields into a ColdFusion struct, because, as I said, CFWheels does it. I just have no idea how to make my application do it.

Here is part of the form that I am talking about

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多孤肩上扛 2024-10-26 12:05:33

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>

Adam had the right context, but his code snippet was wrong.

A function that will work is this:

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

I tested it in my application (outside of CFWheels) and it worked perfectly. All you do is pass in a struct (in my case the Rc struct from FW/1) containing what should be structures, but displaying as strings and you will be returns a structure with nested structures.

Example:

<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>
不疑不惑不回忆 2024-10-26 12:05:33

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

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>

So the most basic form of the change you need to do is this:

mystruct.name = form["report[name]"];

What you need to do is write a loop that loops over the form struct and parses the form field names and builds structures like these. I'm guessing it's already written somewhere in CFWheels (as a function), and you could save yourself headache and frustration by just finding it and pulling it out for yourself.

I think this is it, but I'm not sure:

<!--- 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文