Hi to all,
I am trying to implement a component made of the following elements:
- A table
- Some text fields, one for each bean property
- A button to save the changes
- A button to discard the changes
Basically, the user clicks on a row of the table and automatically the fields are feeded with the clicked item.
I have implemented this feature by using a BeanItemContainer for the table and a BeanFieldGroup for the text fields; in addition, I have caught the ValueChangeEvent event of the table to set the bean to the BeanFieldGroup.
Wheneven I click on a row, I set the item data source to the field, but this way Vaadin adds the validators each time I click a row. As a result, if I have a validation error, the message results as duplicate.
To solve the problem, I had to manually remove the validators. Is there any other solution? Am I missing something?
It would be nice if the BeanFieldGroup would clear its validators before setting the new bean.
I am using Vaadin 7 nightly build (2012/12/01).
Thank you.
Here it is a snippet of code:
final BeanFieldGroup<LocaleVO> form2 = new BeanFieldGroup<LocaleVO>(LocaleVO.class);
form2.setBuffered(true);
table.addValueChangeListener(new Property.ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
final LocaleVO item = (LocaleVO) table.getValue();
for (Field f : form2.getFields()) {
f.removeAllValidators();
}
form2.setItemDataSource(new BeanItem<LocaleVO>(item));
}
});
table.setSelectable(true);
table.setImmediate(true);
I have found the problem in this snippet of code from BeanFieldGroup:
protected void configureField(Field<?> field) {
super.configureField(field);
// Add Bean validators if there are annotations
if (isBeanValidationImplementationAvailable()) {
BeanValidator validator = new BeanValidator(beanType,
getPropertyId(field).toString());
field.addValidator(validator); // This will add a lot of validators...
if (field.getLocale() != null) {
validator.setLocale(field.getLocale());
}
}
}