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

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

setRequired(false) in Vaadin 8

Filip Herceg
6 years ago Mar 27, 2017 2:35pm

Hi everyone,
I'm starting my first project in Vaadin 8 (after ~10 successful projects in Vaadin 7) and I have noticed that fields no longer have the setRequired() method.
In Vaadin 7, if I wanted to set a field to be required/not-required, all I had to do was call setRequired() and that would implicitly add "required" validation to the field.
The closest thing in Vaadin 8 that I have found to the setRequired() method is asRequired():

binder.forField(nameField).asRequired("Every employee must have a name").bind("name");

This works as expected, but what I want to know is: If I call asRequired() and bind a field, how do I later say that the field is no longer required?
Is there a way to remove the "required" Validator (or any other Validator) after I bind a field?
Often times in the applications I develop, it is necessary to set a certain field to be required/not-required depending on the value that the user enters in some other field in the form.

Nicklas Karlsson
6 years ago Mar 28, 2017 5:21am
Roland Polzer
6 years ago Apr 06, 2017 10:32am
Luca Pertile
5 years ago Jun 07, 2017 12:56pm
Luca Pertile
5 years ago Jun 07, 2017 3:08pm
Andrea Galvagni
5 years ago Jul 15, 2017 1:42pm

Same problem here, I have ended up recreating the Binder every time I need to change the .asRequired and .withValidator options... not optimal...

Also, it seems to me that with Vaadin 8 one ends up with three "versions" of the state for each field, namely:

  • the state of the underlying bean;
  • the state of the binder;
  • the state of the textField.

Which may or may not be the same. Am I missing something?

Andreas Claus
5 years ago Jul 17, 2017 3:56pm
Andrea Galvagni
5 years ago Jul 17, 2017 4:02pm
Teemu Suo-Anttila
5 years ago Jul 18, 2017 1:12pm

There is a way to make a dynamic required error message using 

   .asRequired(context -> /* insert your method returning a String here */)...;

the context variable is an instance of ValueContext, which contains information like the currently used locale, the component and its value.

We had a discussion at some point about changing the required validator to check for the isRequiredIndicatorVisible() and skip the validation if it was false, but this would have some unexpected side-effects for some users.

The asRequired is implemented using the normal withValidator method with the following Predicate

value -> !Objects.equals(value, field.getEmptyValue())

To make your own RequiredValidator that checks if the required indicator is visible, only requires one if clause wrapping the predicate above.

Andreas Claus
5 years ago Jul 19, 2017 2:46pm