ColdFusion Convert Form Values ​​to Structures

I am creating a form for my ColdFusion application using a report of the name format [{field-name}], which when using RoR or CFWheels would give me a structure on a server called a report containing all my field names. I use FW / 1, so all the fields of my form fall into the RC region, and do not remain in the form region. I know that you can convert form fields to a ColdFusion structure because, as I said, CFWheels does this. I just don’t know how to make this application.

Here is the part of the form I'm 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> 
+4
source share
2 answers

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

The function that will work is as follows:

 <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 CFWheels) and it worked perfectly. All you do is pass a structure (in my case Rc struct from FW / 1) containing what should be structures, but displayed as strings, and you will return the 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> 
+4
source

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

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

What you need to do is write a loop that iterates over the structure of the form and analyzes the names of the form fields and builds such structures. I assume that it is already written somewhere in CFWheels (as a function), and you can save yourself the headache and frustration by simply finding it and pulling it out for yourself.

I think that's all, 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> 
+3
source

Source: https://habr.com/ru/post/1341827/


All Articles