Chris Suen
Chris Suen

Reputation: 1

Can't enter space in textfield using forced uppercase textfield-formatter add-on in Vaadin 8

With the TextField Formatter add-on for Vaadin 8, I can do the following to allow only upper-case characters:

Options options = new Options();
options.setBlocks(dataLen);
if (format[1].equalsIgnoreCase("UPPER"))
    options.setForceCase(ForceCase.UPPER);
new CustomStringBlockFormatter(options).extend(field);

However, after setting forced uppercase I can't enter space characters any longer. Does anyone how I can allow spaces as well as forced upper case characters?

Upvotes: 0

Views: 329

Answers (1)

ollitietavainen
ollitietavainen

Reputation: 4275

The TextField Formatter add-on doesn't support arbitrary groupings.The setBlocks in method in Options determines the fixed space-separated groups, so you can specify e.g. options.setBlocks(3,3,2) to allow entering text in the format of XXX XXX XX. If you want to allow only capitals, but spaces allowed in the middle in any position, like so that both HELLO WORLD and HI WORLD are allowed, you can use a CSS trick to set the letters print in all uppercase. Add the following rule in your theme .scss file:

  input.upper-textfield {
    text-transform:uppercase;
  }

and define your TextField without a formatter, like this:

  TextField textField = new TextField("only upper");
  textField.setStyleName("upper-textfield");
  textField.setMaxLength(dataLen);
        

Now you just need to remember to change the text to uppercase in Java as well when you read it:

  String value = textField.getValue();
  if (value != null) {
    value = value.toUpperCase();
  }

Upvotes: 3

Related Questions