Reputation: 12459
I am creating a form with a select-box in my web application. I am using the DropDownChoice + ChoiceRenderer combination in the below described way. It works fine, but for one thing - in does not preselect default values.
I have been struggling with this for quite a long time. I have found several examples on the Internet which were said to work but they didn't work for me.
My codebase looks like this (unrelated parts have been omitted or simplified to improve clarity):
Business Entities
public class MultivalueType
{
private Long id;
private String label;
private String description;
public MultivalueType(Long id, String label, String description)
{
this.id = id
this.label = label;
this.description = description;
}
public Long getId()
{
return id;
}
public String getLabel()
{
return label;
}
public String getDescription()
{
return description;
}
}
public class PropertySettings
{
private Long id;
private String property;
private MultivalueType multivalueType;
public PropertySettings(Long id, String property, MultivalueType multivalueType)
{
this.id = id;
this.property = property;
this.multivalueType = multivalueType;
}
public MultivalueType getMultivalueType()
{
return multivalueType;
}
}
DAO
public class PropertySettingsDao
{
...
@Override
public PropertySettings load(Long id)
{
String query = " ... query ... ";
Object[] params = { id };
return getJdbcTemplate().queryForObject(query, params, getRowMapper());
}
...
}
Form class
PropertySettings property = propertySettingsDao.load(propertyId);
IModel<PropertySettings> formModel = new CompoundPropertyModel<PropertySettings>(property);
Form<PropertySettings> form = new Form<PropertySettings>("editPropertyForm", formModel)
{
@Override
protected void onSubmit()
{
PropertySettings propertySettings = this.getModelObject();
...
}
};
form.add(createEnumSelectbox(multivalueTypeDao, new PropertyModel<MultivalueType>(property, "multivalueType"), "multivalueType", true));
add(form);
Creating select-box
protected DropDownChoice<MultivalueType> createEnumSelectbox(DaoForEntityWithSurrogateKey<MultivalueType> dao, IModel<MultivalueType> model, String componentName, boolean required)
{
// create the model
IModel<List<MultivalueType>> choices = createModelForListView(dao);
// prepare the select-box renderer
ChoiceRenderer<MultivalueType> renderer = new ChoiceRenderer<MultivalueType>("label", "id");
// create the select-box component
DropDownChoice<MultivalueType> selectBox = new DropDownChoice<MultivalueType>
(
componentName,
model,
choices,
renderer
);
// mark the select-box as a required form field
selectBox.setRequired(required);
return selectBox;
}
protected IModel<List<MultivalueType>> createModelForListView(final DaoForEntityWithSurrogateKey<MultivalueType> dao)
{
return new LoadableDetachableModel<List<MultivalueType>>()
{
@Override
protected List<BO> load()
{
return dao.loadAll();
}
};
}
Note that I'm setting the default MultivalueType instance (redundantly) both as a CompundPropertyModel of the Form component and directly as a model of the DropDownChoice component. Based on what I have found on the Internet, this should be enough for the select-box to preselect the corresponding value when the page loads, but it does not work.
I have verified that the property variable in the Form component (obtained from the DAO) really contains valid data.
Also note that everything works fine but for the preselecting - I'm getting correctly populated propertySettings variable in the onSubmit method of the form.
Upvotes: 2
Views: 4230
Reputation: 12459
I've finally managed to find the problem.
I've realized that when I display the form, Wicket logs the following warning:
Model returned object ... which is not available in the list of selected objects.
Based on that error message I've found out that in order for the preselecting of default options to work, the Business Entities must override the equals method (see the corresponding JIRA ticket). At least in Wicket version 1.5.4, which I'm using.
Hope this helps others who will get stuck with the same problem!
Upvotes: 5