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.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
setRequired(false) in Vaadin 8
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.
Thanks!
Haven't tried but can it just be re-bound? Or can you have a custom validator that does the required validation based on the parameter?
I would need that too. Re-binding creates additional bindings. Had to re-build the complete binder
I whish it me too!!!
Use validation doesn't display required mark!
Ok, I solved (but I'm not happy about the way), using method setRequiredIndicatorVisible() of widget to dinamically display required mark. Then I set a validator like the following, on each need field:
public ValidationResult validateRequired(String value, ValueContext context) {
Component c = context.getComponent().orElse(null);
if (c != null && c instanceof HasValue && ((HasValue<?>) c).isRequiredIndicatorVisible()
&& (StringUtils.isBlank(value)))
return ValidationResult.error("Value required for " + c.getCaption()));
return ValidationResult.ok();
}
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?
Hello,
I have the same problem here.
Here is a shorted code snippet of an existing Vaadin-7-app:
private TextField orgName = new TextField();
public void localeChanged(Locale newLocale) {
this.orgName.setRequiredError(i18n.get(I18nKeys.ORG_NAME_MISSING));
}
private void setManualOrganizationValuesEntry(boolean manualEntry) {
this.orgName.setRequired(manualEntry);
}
This is really hard to translate into a Vaadin-8-app.
Also because the setter for the error messages are not available (in Vaadin-7 there was a AbstractValidator.setErrorMessage method, in Vaadin-8 not any more).
Do you know how the change the validation-message of a binder afterwards?
Thanks,
Andreas
As far as I know, the only way to change the error message in Vaadin 8 is by utilising a binder, e. g.
binder.forField(textField).asRequired("Error Message").withValidator(someValidator).bind("propertyKey")
But I would love to be shown it can be done in another way...
There is a way to make a dynamic required error message using
binder.forField(textField)
.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.
Hello Teemu,
thank you very much for your tip.
This was very helpfull.
Best regards,
Andreas