Veto against new item in combobox

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);
    }
  }

How shall the validator know that there was a wrong value entered when you don’t let it know?
In your “addNewItem” method you prevent the addition of unwanted values by yourself. So you also have to set the error flag by yourself.

[quote]
because internally the value has not changed to “sddfd”, but to the first predefined item.
[/quote]Just look in the “else” branch of your method. There the first predefined value ist set.

I know, that I set it back to the first item in my list, but the problem I have, is when the user removes all characters in the combobox using “Backspace”, the field is empty in the Browser (but not on the server), although null values are not allowed and this seems to be attached to the problem, that I allow new values.
If I do not allow new values, the field gets reset to the last value (in the browser and on the server) after clearing the field with backspace.