Reputation: 1559
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.
Upvotes: 2
Views: 2339
Reputation: 8414
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.
Upvotes: 1
Reputation: 1866
@Stealth - since my answer is little big i added a new answer.
If you have dynamic input's being added in the form, then i can think of one solution is have the complete input list as a json string and send the complete json string in the request to the server. For example you can have the names of the input in your own convention, as you currently have "choiceList", you can have the inputs as "choiceList1", "choiceList2" etc, you can maintain the existing counts and when you are creating new texts you can increment the counter and form the new name. For example on load of form you have 2 fields then the counter is 2, when creating a new field using js then the counter becomes 3 and the text field name will be "choiceList3". Now before submit you should call a javascript method which iterates all the input fields and form the json string as [{"choiceList1": "apple, mango, orages", "choiceList2": "pear", "choiceList3": "strawberry"}]
. This whole string can be submitted to the backend and you can use process it in the backend.
Upvotes: 1
Reputation: 1866
If the names of the input type is same, in your case its "choiceList" so ideally the http request does by appending the both the input with comma seperated. Ideally you should have 10 different text fields with different names for example choice1, choice2 etc and get whatever input they are entering and process accordingly in the backend. This is my thought.
Upvotes: 0
Reputation: 31795
You can use @InitBinder annotation to configure binding for string array:
@InitBinder("choiceList")
public void fqBinderInit(WebDataBinder b) {
b.registerCustomEditor(String[].class, new StringArrayPropertyEditor("&"));
}
Upvotes: 2