We have a special case, where we have a combobox in our gui to select a separator character (needed for CSV file parsing). Naturally the separator is a required field, but it is possible to add a new separator character as we by default provide just the normal ones as defaults in the combobox (like semicolon, etc.)
So we set:
setNullSelectionAllowed(false);
setFilteringMode(FILTERINGMODE_STARTSWITH);
setImmediate(true);
setNewItemsAllowed(true);
setNewItemHandler(this);
setInvalidAllowed(true);
setValidationVisible(true);
setInvalidCommitted(true);
setRequired(true);
addValidator(new StringLengthValidator("only 1 char allowed", 1, 1, false));
The addNewItem only adds something if the input is only 1 char long.
The problem now is, that if somebody enters a String like “sddfd” into the combobox it is accepted and no(!) validation error is shown, because internally the value has not changed to “sddfd”, but to the first predefined item.
If we disable the newItemsAllowed, it works correctly. So, is there a way in the addNewItem() method to veto against a given input or at least redraw the input field with the current value?
Our addNewItem method looks like this:
@Override
public void addNewItem(String newItemCaption)
{
Character chr';
if (newItemCaption != null && newItemCaption.length() == 1)
{
chr = newItemCaption.charAt(0);
if (!containsId(chr))
{
values.add(chr);
addItem(chr);
setValue(chr);
}
}
else
{
setValue(values.get(0),false);
}
}