Vaadin8 field validation without binders.

I am attempting to upgrade to Vaadin8. I have yet to find a clean way to address a situation where I basically I have just
one
textfield in a window that requires validation.

How can this be acheived without creating a wrapper bean and then using the binder ?

I had a similar issue, this is my solution (“binding” to a String variable):

....
    private final Binder<String> binder = new Binder<>();

    private final TextField nameField;

    private String name = "";

    public NameDialog(String name, Validator<String> nameValidator) {
        ...
        nameField = new TextField("Name", name);
        nameField.setWidth(100, Unit.PERCENTAGE);
        binder.forField(nameField).withValidator(nameValidator).bind(s -> this.name, (s, v) -> this.name = v);
        addComponent(nameField);
        ....
    }
....

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

Alejandro,

Sure that works, but its a hack to get around the fact that to validate fields you are now REQUIRED to use data binding and have a backing Bean to bind the fields to.

Why were these two ideas binding and validation linked like this, surely it should be possible to add validators to components that do not use data binding.

I think I understand why you see it as a hack. But I wouldn’t say it’s a hack at all. It’s just a new API. In my case it was a bit hard to “forget” about the V7 API, but when I see the new one as a whole, then it makes sense.

The idea of putting validation under the binding umbrella is to offer developers a flexible way of chaining validators and convertors. If validators were included in the field itself, it would be impossible to define validators both before and after conversion.

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

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

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

I would like your orientation in this issue, since I didn’t receive an answer:
https://vaadin.com/forum#!/thread/16253506