Re: FormMaps and indexed elements/collections

"Diez B. Roggisch" <[email protected]>
Newsgroups gmane.comp.java.enhydra.barracuda.general
Message-ID <[email protected]>
Bradley Hill wrote:
>   I haven't seen any examples or mention of it in the mail archives, but this 
> has to be a common issue.
>   
>   I'm looking to create forms with indexed property sets, e.g. name1, name2, 
> nameN, value1, value2, valueN.  I don't know until runtime how many of these 
> properties there will be.  
>   
>   Is there a way to handle arrays and collections of values in the Barracuda 
> forms validation package?  Struts will recognize and map parameters with names 
> like "name[n]" and insert them properly into beans with indexed getter/setter 
> methods.  Is there similar functionality in Barracuda or would it be easy for me 
> to add?

If you want to map and validate such values, things are easy: As HTML 
doesn't know anything about indexed/mapped values, you will have to 
create distinct names for every entry in your collection. E.g. for a 
list you do

<input type="text" name="mylist_1" value="foo"/>

Its no problem to create a FormMap with a element for every single of 
these input fields.

The only thing missing is a convinience-function to access such a list 
of values. But thats not too hard - simply extend DefaultFormMap (or 
RepopulationFormMap) with methods like these (beware of pseudo-code):

public Map getElementAsMap(String prefix) {
	Map result = new HashMap();
         for(FormElement element in getElements()) {
            if(element.getName().startsWith(prefix)) {
               result.put(element.getName().substring(prefix.length() + 
1), element.getVal());
            }
         }
         return result;
}

For a list, you want to convert the keys to ints and put the values into 
the result at the specified position.


Now, for rendering such stuff, things get a little bit more complicated. 
For every mapped/indexed value, you have to create a IterativeModel, so 
you can populate its data.

I could imagine the 2 new FormElements for the RepopulationFormMap could 
add your functionality quite easy - look into CheckBoxGroupElement. Then 
you could even access the value directly as Map/List with 
getElement(name) on RepopulationFormMap.


Regards, Diez
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.