form-serialize
form-serialize copied to clipboard
Infer arrays from duplicate nested names
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?
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 />