Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC Form tags: Is there a standard way to add "No selection" item?

There is a select dropdown and I want to add "No selection" item to the list which should give me 'null' when submitted. I'm using SimpleFormController derived controller.

protected Map referenceData(HttpServletRequest httpServletRequest, Object o, Errors errors) throws Exception {     Map<String, Object> map = new HashMap<String, Object>();      map.put("countryList", Arrays.asList(Country.values()));      return map; } 

And the jspx part is

<form:select path="country" items="${countryList}" title="country"/> 

One possible solution seems to be in adding a null value to the beginning of the list and then using a custom PropertyEditor to display this 'null' as 'No selection'. Is there a better solution?

@Edit: I have solved this with a custom validation annotation which checks if the selected value is "No Selection". Is there a more standard and easier solution?

like image 472
axk Avatar asked Oct 02 '08 14:10

axk


1 Answers

One option:

<form:select path="country" title="country" >      <form:option value="">&nbsp;</form:option>      <form:options items="${countryList}" /> </form:select> 
like image 159
Jacob Mattison Avatar answered Sep 18 '22 16:09

Jacob Mattison