Introducing Vaadin: Validation

Let's talk about validation in Vaadin.

Context

We want to have a textfield which is marked as a "required" field and which expects integer values from 0 - 99. The user should get feedback right after he entered something into the field.

Implementation

Here's the complete code of the module:

@SuppressWarnings("serial")
public class MyVaadinUI extends UI
{
    @Override
    protected void init(VaadinRequest request) {
        final TextField textField = new TextField("Enter an Integer value (0-99)");
        final FormLayout mainLayout = new FormLayout(textField);
        textField.setConverter(Integer.class);
        textField.addValidator(new IntegerRangeValidator("only integer, 0-99", 0, 99));
        textField.setRequired(true);
        textField.setImmediate(true);
        setContent(mainLayout);
    }
}
-----
1 Class
7 Lines of active code
4 lines of code configuring the TextField (!)


Result

A red asterisk marks the field as required. After the user entered something into the field and the focus on the field is lost there is immediate feedback for the user.







Does it look pretty? meh..
Does it work at a minimum of spent work and time? 4 lines of code?! Yea!

Kommentare

Beliebte Posts