I have a jsp/html form that collects an array of choices from user. For example, I ask “what did you like about this food?” and the user can add 1 answer per input field (with option to put in unlimited number of choices).
Here is what the html looks like:
<li><input type="text" name="choiceList[]" /></li>
<li><input type="text" name="choiceList[]" /></li>
<li><input type="text" name="choiceList[]" /></li>
<li><input type="text" name="choiceList[]" /></li>
<li><input type="text" name="choiceList[]" /></li>
…
In my backend (which is using Spring) I assign the choiceList array to a String[] choices java array.
My problem with this is that if somebody puts a comma separated list in one of the input fields (for example: apple, mango, orange”) and “pineapple” in another input field, the backed java array thinks the user input was 4 separate items (apple, mango, orange, pineapple) because the html form array choiceList[] is basically a comma separated list.
What do I need to do in my form so I can really tell that the user filled out only 2 input fields? To be clearer, I want my java array to be of size 2 not of size 4.
I basically had the same issue a while back and asked this question SO: How to prevent parameter binding from interpreting commas in Spring 3.0.5?
The accepted answer will allow you to prevent Spring interpretting the commas in the users’ input.