Robert Strauch
Robert Strauch

Reputation: 12876

Restrict input in Vaadin IntegerField when typing

I have a simple IntegerField in Vaadin 14 with the following settings:

final IntegerField delayField = new IntegerField();
delayField.setMin(0);
delayField.setMax(600000);
delayField.setStep(1000);
delayField.setHasControls(true);

binder.forField(delayField).bind(AppConfiguration::getNotificationDelay, AppConfiguration::setNotificationDelay);

Using the step controls it is not possible to enter values lower than 0 or higher than 600,000. But if I type a value directly, then values like -52 are possible. My understanding was that setMin() and setMax() also works for user input.

How can I automatically restrict the input to the min/max values?

Upvotes: 1

Views: 384

Answers (1)

Simon Martinelli
Simon Martinelli

Reputation: 36103

There is even a simpler solution. Just change the value in the ValueChangeListener:

delayField.addValueChangeListener(event -> {
        if (event.getValue() < 0) {
            integerField.setValue(0);
        } else if (event.getValue() > 600000) {
            integerField.setValue(600000);
        }
    });

Upvotes: 4

Related Questions