Vaadin 8 validation using binder or

Hi,

I am writing validation for a text field in vaadin 8. Earlier i used
addValidator
in Vaadin 7 but now in Vaadin 8 it is removed from the api. I tried like the code below in Vaadin 8 but i am not getting error message in red colour. It will be really helpful if someone can give me insights about this.

Thanks!

  txtUser.addValueChangeListener(valueChangeEvent -> {if (valueChangeEvent.getValue().isEmpty())
{txtUser.setPlaceholder("Error: May not be null" );}

You need to add you validator on binding.

binder.forField(username).withValidator(new StringLengthValidator(invalidLengthMsg, 1, 255)).bind("username");

thanks for replying. I tried this earlier but it was asking me to create a bean which i am not aware of.

Binder<MonitorUI> binder = new Binder<>();
binder.forField(txtUser) .withValidator(new StringLengthValidator( "Error: User ID must be 3-30 (was {0})", 3, 30)).bind("txtUser");

So you have a GUI without binding to persisted data?

I think now validation is available only with binding: to get red error message you have to produce a ValidationResult, but this is good only in binding mechanism. You should evaluate to create a fake binding to a java object, or implement the solution above with listener and display a notification (Notification.show)

It seems to me that the binder concept is optimized for cases where the user interface data is directly represented by backing data. But for the more general case where the UI refers to a business logic layer, and the business layer refers to the persisted data, then binder is an unwanted short circuit between UI and data. And the overhead of creating and managing beans for a “form” is tedious and not always practical for dynamic UI presentation that may change structure due to context, time or data. I too have looked for the best approach to achieve some validation without binder. My own approach is to use a value change listener then take action based comparison with a regex. For a validation result more like in V7, the
discussion here
is useful.

finally found a way to do it -

public Label messageLabel = new Label(“”, ContentMode.HTML);

txtField.addValueChangeListener(valueChangeEvent → {if (valueChangeEvent.getValue().isEmpty()) {messageLabel.setValue(String.format(“Error: May not be null”));}