Adding an empty option is very easy to do in normal HTML/JSP page. But I am using spring roo and the GUI tag libraries it offers. I need to add an empty option to the combox and be able to validate if a user has slected a non empty option in this combo box or not. The code generated by Spring roo doesn't have this and first option of my combo box appears to be a non empty option. Any idea how to extend this behavior and how to do the validation or client and server sides?
Spring roo by default creates a combo box with widgetType "dijit.form.FilteringSelect" applied on it , this combo box doesn't contain an empty option.
To add an empty option to it please follow the below steps
In your java code when you are preparing a List for that combo box try to add first option with
code =0 and value = -- Not Selected
and then add all the other options in that list.
A simple java code snipet I am using is
ComboDTO comboDTO=new ComboDTO();
comboDTO.setCode("0");
comboDTO.setDescription("--Not Selected--");
list.add(comboDTO); //This is the list which will be used to populate values in combo box
The option having the code = 0 will be shown as the first element in your combo box. Now if you want it to be mendatory field implement your Validator in and check if the option slected in combo has code=0 send an error back
Sample code for checking the value and sending error back from validator is like below
if(myObject.getFieldCode().equals("0")){
errors.rejectValue("fieldCode", "required.fieldCode");
}
This way you can have a combo box with an empty option and can check the validity at server side.
Unfortunatly I couldn't find a way to show dojo type notification at client end if the user has not selected a valid option in the combo box. But this is working for me now.