Search code examples
validationvaadinvaadin8

Invoke validation user-feedback without binder & bean in Vaadin 8


In Vaadin 8, the built-in validation feature is established with a Binder hooked up to a bean, as discussed in the Manual and as shown in this Answer. I understand how this works, and it works well, I appreciate the Vaadin team’s thinking and effort there.

However… I just want a simple quick-and-dirty data-entry field accepting only -1, 0, & 1 as values. I like the Vaadin validation’s feature of rejecting bad input by marking the field/caption with a bang and red color and so on.

➥ Is there some way to invoke the validation-style feedback to user without going to all the elaborate trouble of defining a bean and establishing a binder?

As a workaround, I could replace my data-entry field with a pop-up menu (a.k.a. drop-down list). But my question is still useful for all the times when we need quick simple input from user such as in a dialog box, without all the ceremony of a data-entry form.


Solution

  • Binder is not designed for single field use.

    Vaadin 8

    The best alternative to do single-field validation with Vaadin 8, is just do hook with ValueChangeListener of the field and do what ever is needed in value change event.

    AbstractComponent::setComponentError(ErrorMessage componentError)

    Note, however in Vaadin 8 field components do have setComponentError(..) method. By calling this method, your single-field validation with ValueChangeListener by setComponentError(…) you have the same look and feel analogous to a binder in a form. This works with textfields, buttons, and so on.

    See the Handling Errors page of the manual.

    button.setComponentError( new UserError( "Bad click" ) ) ;
    

    Vaadin 7

    There is a difference between Vaadin 7 and Vaadin 8. With Vaadin 7 it was possible to assign validator directly to a field.