form with multiple instances of the same fields names

63 views
Skip to first unread message

E F

unread,
Jan 21, 2013, 4:06:53 PM1/21/13
to cfwh...@googlegroups.com
Hi,

I have a form where the user can add rows to it if they need to add another record. As a result of this, i have form fields with the same name. Here is a copy of the cfdump to better illustrate what I mean.

struct
BUILDINGID1,1
COLOURyellow,red
MAKEFerrari,Chevy
MODELTestaroda,Suburban
PLATEAPSW211,VE3OBX
SPOTNUMBER22,23
TENANTID790,790
TYPECar,Car
YEAR2012,1978



As you can see coldfusion creates a list of the form names for me. I am able to deal with this outside of the wheels framework by a few loops and inserts queries. That said, how would I do an insert or update in the cfwheels framework in this scenario.

Thanks for any guidance,
Erik

Andy Bellenie

unread,
Jan 21, 2013, 4:20:39 PM1/21/13
to cfwh...@googlegroups.com
I'd add an index to the form field name to diffentiate between them.

E.g. textFieldTag(name="buildingId[1]")

On post, Wheels will format this into a structure for you.

--
You received this message because you are subscribed to the Google Groups "ColdFusion on Wheels" group.
To view this discussion on the web visit https://groups.google.com/d/msg/cfwheels/-/taf66XXdmRAJ.
To post to this group, send email to cfwh...@googlegroups.com.
To unsubscribe from this group, send email to cfwheels+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cfwheels?hl=en.

Erik Fenkell

unread,
Jan 21, 2013, 4:43:22 PM1/21/13
to cfwh...@googlegroups.com
Like this....

#textFieldTag(name="resident[make][i]", value=resident.make)#</td>

Erik Fenkell

unread,
Jan 21, 2013, 4:55:54 PM1/21/13
to cfwh...@googlegroups.com
I tried this and it does not wokr. Is this what you mena though?

                        <!--- ROW 1 ---->
                        #hiddenFieldTag(name="resident[tenantid[1]]", value=resident.tenantid)#
                        #hiddenFieldTag(name="resident[buildingid[1]]", value=resident.buildingid)#
                        <td>#textFieldTag(name="resident[spotNumber[1]]", value=resident.spotNumber, class="input-mini")#</td>
                        <td>#textFieldTag(name="resident[type[1]]", value=resident.type, class="input-mini")#</td>
                        <td>#textFieldTag(name="resident[plate[1]]", value=resident.plate, class="input-mini")#</td>
                        <td>#textFieldTag(name="resident[make[1]]", value=resident.make, class="input-small")#</td>
                        <td>#textFieldTag(name="resident[model[1]]", value=resident.model, class="input-small")#</td>
                        <td>#textFieldTag(name="resident[year[1]]", value=resident.year, class="input-mini")#</td>
                        <td>#textFieldTag(name="resident[colour[1]]", value=resident.colour, class="input-mini")#</td>


                         <!--- ROW 2---->
                        #hiddenFieldTag(name="resident[tenantid[2]]", value=resident.tenantid)#
                        #hiddenFieldTag(name="resident[buildingid[2]]", value=resident.buildingid)#
                        <td>#textFieldTag(name="resident[spotNumber[2]]", value=resident.spotNumber, class="input-mini")#</td>
                        <td>#textFieldTag(name="resident[type[2]]", value=resident.type, class="input-mini")#</td>
                        <td>#textFieldTag(name="resident[plate[2]]", value=resident.plate, class="input-mini")#</td>
                        <td>#textFieldTag(name="resident[make[2]]", value=resident.make, class="input-small")#</td>
                        <td>#textFieldTag(name="resident[model[2]]", value=resident.model, class="input-small")#</td>
                        <td>#textFieldTag(name="resident[year[2]]", value=resident.year, class="input-mini")#</td>
                        <td>#textFieldTag(name="resident[colour[2]]", value=resident.colour, class="input-mini")#</td>
                   

Erik Fenkell

unread,
Jan 21, 2013, 4:58:22 PM1/21/13
to cfwh...@googlegroups.com
Can wheels really figure this out with out looping through the fields?

Chris Peters

unread,
Jan 21, 2013, 5:04:50 PM1/21/13
to cfwh...@googlegroups.com
Try keys that look similar to this:
resident[tenantid][1]

Yes, it works. Try submitting your form and dumping the params struct.

Erik Fenkell

unread,
Jan 21, 2013, 5:11:17 PM1/21/13
to cfwh...@googlegroups.com
struct
BUILDINGID
struct
11
21
COLOUR
struct
1White
2 Orange
MAKE
struct
1Ford
2Hyundai
MODEL
struct
1f-150
2Pony
PLATE
struct
1qweqwe
2 ddsaf
SPOTNUMBER
struct
125
226
TENANTID
struct
11648
2 1648
TYPE
struct
1Truck
2Car
YEAR
struct
12012
2 1981

The cfdump is what I get when I try that.

When I try to insert it into the DB I get the following error.

Complex object types cannot be converted to simple values.


My insert code is <cfset model("vehicles").create(params.resident)>

Am I supposed to be doing something else with that structure.

Thanks for your help Andy, it is appreciated.
Erik

tpet...@gmail.com

unread,
Jan 21, 2013, 7:42:09 PM1/21/13
to cfwh...@googlegroups.com
you will need to do a loop and collection to similar conditions into a struct of their own:

// pick a key in the struct and see how many elements it has
// from your example you should have 2 elements in each key
// of the struct
<cfset counter = StructCount(params.resident.buildingid)>
// loop from 1 to the number of elements. in your example 2
<cfloop from="1" to="#counter#" index="i">
  // create a struct to hold the key value pairs we gather from each
  // element
  <cfset args = {}>
  // loop over each key in the params.resident struct
  <cfloop collection="#params.resident#" item="r">
    // see if the element exists in the key
    // in other words we're checking for
    // params.resident['buildingid'][1]
    <cfif StructKeyExists(params.resident[r], i)>
       // if it exists. add the key and the value to
       // our args struct that we created
      <cfset args[r] = params.resident[r][i]>
    </cfif>
    // call create of the model passing our
    // args struct in place of the params.resident
    <cfset model("vehicles").create(args)>
  </cfloop>
</cfloop>

Erik Fenkell

unread,
Jan 22, 2013, 2:45:10 PM1/22/13
to cfwh...@googlegroups.com
Thanks for this. I am very close. I was able to get my structureall setup.

I get the the following error:

Element model is undefined in a CFML structure referenced as part of an expression.


My data looks like this:

struct
BUILDINGID
struct
01
11
21
COLOUR
struct
0Green
1Silver
2Black
MAKE
struct
0Ford
1 Lexus
2Porsche
MODEL
struct
0Escort
1F7
2928 C
PLATE
struct
0AHH123
1MKL291
2HTWHLS
SPOTNUMBER
struct
022
1 23
224
TENANTID
struct
01648
11648
21648
TYPE
struct
0Car
1 Car
2Car
YEAR
struct
02011
12010
21970

E F

unread,
Jan 23, 2013, 5:08:14 PM1/23/13
to cfwh...@googlegroups.com
I Got it working with the following code. I am sure there is a better way, but this made sense to me.

 <cfset counter = StructCount(params.resident.buildingid)>
        <cfloop from="1" to="#counter#" index="i">
            <!--- REMEBER TO SUBRACT ONE BECAUSE  THE ARRAY INDEX STARTS AT 0 --->
            <cfset i = #i#-1>
            <cfoutput>
                <cfset newVehicle = model("vehicles").new()>
                <cfset newVehicle.tenantid = #params.resident.tenantid[i]#>
                <cfset newVehicle.buildingid = #params.resident.buildingid[i]#>
                <cfset newVehicle.spotnumber = #params.resident.spotnumber[i]#>
                <cfset newVehicle.type = #params.resident.type[i]#>
                <cfset newVehicle.plate = #params.resident.plate[i]#>
                <cfset newVehicle.make = #params.resident.make[i]#>
                <cfset newVehicle.model = #params.resident.model[i]#>
                <cfset newVehicle.year = #params.resident.year[i]#>
                <cfset newVehicle.colour = #params.resident.colour[i]#>
                <!--- CHECK TO SEE IF THE RECORD SHOULD BE ADDED OR AN EXISTING ONE UPDATED --->
                <cfif NOT isDefined('params.resident.id')>
                    <cfset newVehicle.save()>
                <!--- IF THERE IS A NEW RECORD TO BE INSERTED AMONGST RECORDS THAT ARE TO BE UPDATED --->
                <cfelseif #params.resident.id[i]# EQ 0>
                    <cfset newVehicle.save()>
                <cfelse>
                <!--- UPDATE AND EXISTING RECORD --->   
                    <cfset result = model("vehicles").updateByKey(#params.resident.id[i]#, newVehicle)>
                    <cfdump var="#params.resident#"/> 
                </cfif>           
            </cfoutput>
        </cfloop>  

Thanks again for everyone's help, it got me to my solution!

Erik

Reply all
Reply to author
Forward
0 new messages