This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

Binding dynamic forms

Nov 22nd, 2005, 01:33 PM

Hi,

There are a lot of threads here about binding dynamic forms to the Command object but I haven't been able to find a real, concrete, clean solution anywhere.

Example scenerio:

I have a command object that has a list of string values. I generate my form and each string value in my list is shown as a textbox. I also have form buttons to add/remove textboxes to/from the form through JavaScript. I add new textboxes and remove some too.

When I submit the form, how do I handle binding the form elements to my command?

Is there a solution for this..?? i searched on on the threads and found this old thread which exactly is my problem...i have a page where i add textboxes dynamically through javascript..when i submit how to bind these values ??

That's true since only the request parameter names matter at the end. The only problem is accessing an index in the list that does not yet exist on the server (more items added on the client). It can be solved with LazyList.

Joerg

Comment

Hi,
I used the LazyList and it works great for adding new records:
with javascript i add new records and they get binded by spring.
My problem is that i want to allow delete record option as well,
i can make it work by overide onBindAndValidate and i read of another solution which use propertyEditor.
apache TreeList sounds intresting:http://commons.apache.org/collection.../TreeList.html
but i could make it work,couldn't find example.

Is someone aware of a more elegant solution?
Thanks.

Comment

That's true since only the request parameter names matter at the end. The only problem is accessing an index in the list that does not yet exist on the server (more items added on the client). It can be solved with LazyList.

Joerg

Hi, I'm not sure if I lack somethings in my codes but I am still encountering problem even if I'm using the LazyList.

My scenario is same with others, I have text box which can be added and deleted using javascript. I have already read this: Dynamic list binding in Spring MVC by matt fleming

For clarity, I have for example a Type bean with list of Subtype. Subtype has name as property. In my add page, I could add names (text fields) and this works. The names are set and Subtype list binding is working properly. The problem is when I'm in my edit page and I have already displayed the Type details (containing list of Subtypes, say count is 4) and I click the add subtype button (in edit page, still can add Subtype) so now the count is 5. But once I submit the page I'm having error similar to this:

..Index of out of bounds in property path 'subType[4]'.. (last added element).

Am I missing something? At my add page the dynamic adding of text fields is working fine, but on edit it's not? Maybe because on edit there's already bound command (Type)? I've read some post here that access the request and see the count of added fields, and from that re-create lacking object (or something like that) against the original count in the formBackingObject() method. Is there a more elegant/better solution than this?

Just for info, I'm relatively new in Spring and I appreciate any advice on this. Thanks in advance.