问题描述
我使用命名格式报告[{field-name}]为我的ColdFusion应用程序构建一个表单,当使用RoR或CFWheels时,会在后端调用一个包含我所有字段名称的报告结构。我使用FW / 1,所以我的所有表单字段放入RC范围,而不是保留在Form范围。我知道,有可能将我的表单字段转换为ColdFusion结构,因为,正如我所说,CFWheels。我只是不知道如何让我的应用程序这样做。
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>
推荐答案
Adam有正确的上下文,但他的代码片段错误。
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>
我在我的应用程序(CFWheels外)测试了它,它工作完美。你所做的就是传入一个结构体(在我的例子中是Rc结构体从FW / 1)包含应该是结构,但显示为字符串,你将返回一个结构与嵌套结构。
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.
示例:
<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>
这篇关于ColdFusion将表单值转换为Struct的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!