Hopefully I can explain this properly. I have a form that gets converted to nested json from grouped form elements such as
<input type="checkbox" name="faults[slats][]" value="cracked"/>
<input type="checkbox" name="faults[slats][]" value="broken"/>
<input type="text" name="partsRequired[0][partDescription]"/>
<input type="text" name="partsRequired[0][size][]"/>
<input type="text" name="partsRequired[0][size][]"/>
<input type="text" name="partsRequired[1][partDescription]"/>
<input type="text" name="partsRequired[1][size][]"/>
<input type="text" name="partsRequired[1][size][]"/>
The resulting parsed json looks like:
{
faults : {
"slats": [
"cracked",
"broken"
]
},
partsRequired: [
{
"partDescription": "Fabric Ochre",
"size": ["1x5m", "1m", "2m"],
"colour": "Ochre",
"quantity": "1"
},
{
"partDescription": "",
"size": "",
"colour": "",
"quantity": ""
}
]
}
I need to be able to loop through this data and repopulate the form. I have a function that recreates the variable names and values in a way that allows me to do that for teh first set of data 'faults'. The issue I am having is that I can't work out how to do it for the second scenario parts required where both are combined.
See fiddle: http://jsfiddle.net/JyfA2/1/. You can see that if you pass the array to the array function it creates the correct data but passing partsRequired saves the key integer for partsRequired[0][size][] which gives me the wrong name to find and populate the element on the form.
How can I get around this?