The question is based on getting a spring model attribute list element using an index from javascript on which explains that each name of the html element must contain an index. Example user[0]
I have a form that allows the user to dynamically add or remove elements from the list before submitting it. Let me put an example: we add 3 users, so the form will now contain 3 inputs:
<input name="user[0].name" type="text" />
<input name="user[0].surname" type="text" />
<input name="user[1].name" type="text" />
<input name="user[1].surname" type="text" />
<input name="user[2].name" type="text" />
<input name="user[2].surname" type="text" />
If we submit this, we'll have 3 users each having a name and a surname. But, the problem comes when we try to remove the user[1] because I end up with this html:
<input name="user[0].name" type="text" />
<input name="user[0].surname" type="text" />
<input name="user[2].name" type="text" />
<input name="user[2].surname" type="text" />
If we submit this form, springs still creates 3 users but the user[1] will have null values for the name and surname.
I know that I can handle input names to always have a "correct" form. But is there another solution? I thought that a form like
<input name="user[].name" type="text" />
<input name="user[].surname" type="text" />
<input name="user[].name" type="text" />
<input name="user[].surname" type="text" />
may solve the problem but then I realized: how can spring know which name comes with which surname.
Any idea on a better solution?