defunctzombie / form-serialize

serialize html forms
MIT License
396 stars 54 forks source link

Infer arrays from duplicate nested names #38

Open greypants opened 8 years ago

greypants commented 8 years ago

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?

sant123 commented 6 years ago

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 />