maxatwork / form2js

Javascript library for collecting form data
http://maxatwork.github.com/form2js/
MIT License
640 stars 137 forks source link

multiselect with name ending with [] #6

Closed ndelargy closed 13 years ago

ndelargy commented 13 years ago

form2js is excellent, searched for this solution for hours and I'm so glad that I didn't have to attempt to write a solution.

I have a multi select marked up as follows

<select name="categories[]" id="categories" multiple="multiple" class="multiple">
    <option value="News" label="News" selected="selected">News</option>
    <option value="Events" label="Events">Events</option>

    <option value="New Products" label="New Products" selected="selected">New Products</option>
</select>

if we have selected News and Events then formTo produces the following object from this... { ... "categories" : [ [ "News", "Events" ] ] ... }

Whereas I required { ... "categories" : [ "News", "Events" ] ... }

I solved the issue for my own needs by changing line 92 in form2object.js from name = nameValues[i].name; to name = nameValues[i].name.replace('[]','');

This doesn't seem to have any negative effects

I'm using Zend Form to create the form which adds the square brackets to the name if the element is expecting an array

maxatwork commented 13 years ago

This solution will have impact on non-select based arrays serialization (like several text inputs with the same name ending with "[]"). Changed form data extraction function for selects instead.

ndelargy commented 13 years ago

Have double checked that the solution works for my application. Awesome work. Thanks again.