I have a freemarker page, where I need to post an Entry from a list of Entries.
In order to overcome problems with spring.formInput and freemarker lists (see problem here) I construct the html forms in the following way:
<#list entries as entry>
<@form.form method="POST" commandName="receiver">
<@spring.formInput "entries.entry[${entry_index}].name"/>
<@spring.formInput "entries.entry[${entry_index}].value"/>
</@form.form>
</#list>
My Controller looks like this:
@RequestMapping(method=POST, params="save")
public String save(@ModelAttribute(value="entries") @Valid Entries entries, BindingResult result, Model model){
/*notice, we always get the last item in the list, since the entries "carries" the index of the form through, and creates
* null-object in the first n places
*/
int index = entries.size()-1;
Entry entry = entries.get(index);
if (!result.hasErrors()) {
formService.save(entry);
}
return "";
}
Unfortunately the @Valid annotations causes error whenever I post an Entry apart from the first in list.
This happens because spring automatically populates my list with null values up till the current index.
- I would prefer to post an Entry instead of Entries, but I do not think this can be done.
- Alternatively, how do I stop the validation from taking place on the first n-1 entries in list?
- Or, can I somehow test which object in list (index) caused the error, so that I can just avoid errors from the first n-1 entries?