form-serialize icon indicating copy to clipboard operation
form-serialize copied to clipboard

Infer arrays from duplicate nested names

Open greypants opened this issue 8 years ago • 1 comments

Values serialize into arrays just fine when they're not nested, but when they are, only the last value is used and serializes as a string:

<input type="text" name="nested[colors]" value="red" />
<input type="text" name="nested[colors]" value="green" checked />
<input type="text" name="nested[colors]" value="blue" checked />

Expected Output:

{ 
  nested: {
    colors: ["green", "blue"]
  }
}

Actual Output:

{ 
  nested: {
    colors: "blue"
  }
}

I can work around it if I explicitly add [] to the names, but it seems like it should work without that. Thoughts?

greypants avatar Jun 16 '16 18:06 greypants

Remember to put brackets at the end to mark it as an array, take a look:

        <input type="checkbox" name="nested[colors][]" value="red" />
        <input type="checkbox" name="nested[colors][]" value="green" checked />
        <input type="checkbox" name="nested[colors][]" value="blue" checked />

sant123 avatar Nov 22 '17 04:11 sant123