Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

Vaadin8 field validation without binders.

Lawal Olufowobi
5 years ago Mar 14, 2017 6:38pm
Ian Scriven
5 years ago Mar 14, 2017 9:49pm
Alejandro Duarte
5 years ago Mar 15, 2017 12:06pm

You can use a ValueChangeListener. For example:

textField.addValueChangeListener(e -> validate(e.getValue()));
...
private void validate(String value) {
    if(... value is valid ..) {
        ...
    } else {
        ...
    }
}

You can reuse an already implemented Validator if you want to. Moreover, you can define an utility method to add validators and show the error in the field:

public class Utils {

    public static void addValidator(AbstractField field, Validator validator) {
        field.addValueChangeListener(event -> {
            ValidationResult result = validator.apply(event.getValue(), new ValueContext(field));

            if (result.isError()) {
                UserError error = new UserError(result.getErrorMessage());
                field.setComponentError(error);
            } else {
                field.setComponentError(null);
            }
        });
    }

}

Then, you can add a validator as follows:

Utils.addValidator(textField, new StringLengthValidator("Not valid", 5, 10));
Paul Henry
4 years ago Apr 20, 2017 12:03am
Alejandro Duarte
4 years ago Apr 20, 2017 8:47am
Paul Henry
4 years ago Apr 20, 2017 9:22pm

There is a small element of "Who moved my cheese" here ill give you that. 

Luca Pertile
4 years ago Jul 07, 2017 7:00am

In my opinion, it would be better to have validation both on field that on binding, with validation on field occurs first. Programmer should choose the best for him. This solution will cover more cases.

jose luis romero
4 years ago Jul 07, 2017 11:13am

Luca Pertile: In my opinion, it would be better to have validation both on field that on binding, with validation on field occurs first. Programmer should choose the best for him. This solution will cover more cases.

What would even better is not to make obsolete previous version of the framework

Matti Tahvonen
4 years ago Jul 12, 2017 12:27pm

FiruzzZ, this is exactly why we kept the compatibility package there. You can keep using the old way still, untill you gradually move to the new APIs. Even though it is deprecated and your Eclipse might show those scary overstrokes there, the old APIs still work just fine.

But we sure should find out all the best practices for Vaadin 8 and if there are some cases (I know there are) that still don't work fluently, add issues to github project and get some new features/bug fixes in.

cheers,
matti
 

jose luis romero
4 years ago Jul 12, 2017 1:13pm