How does the com.vaadin.flow.data.binder.Binder.asRequired(String errorMessage) method work?

According to the documentation, to check if a field is valid and notify the user about an error, you need to use the .withValidator(…) method in conjunction with the .withStatusLabel(…) method or the .withValidationStatusHandler(…) method.
The .asRequired(…) method is a subset of this validation, and unlike the .withValidator(…) method, it does not need to be bound to the .withStatusLabel(…) method or the .withValidationStatusHandler(…) method to display validation results to the user.
In order to display the validation result to the user using the .withStatusLabel(…) method, I need to create a Label object and add it to the form. But studying the sources of the .asRequired(…) method, I did not see the logic that would create a Label and add it to the form.
And, actually, this confused me… Can anyone explain how this works? Maybe I missed something? I want to validate fields as easily and simply as it is done with the .asRequired(…) method - i.e. without creating Labels and adding them to the form (this is rather tedious, and pollutes the code, especially when there are many fields) - with the only difference that validation will not only consist in checking for the presence of a value.

withStatusLabel/withValidationStatusHandler are only needed if you wanna handle the errors in separate components / at a centralized place. By default asRequired is enough and forwards the error to the error message of the field (red text below the field)

Thanks for the info, but that doesn’t answer my question :frowning_with_open_mouth:
At first I thought that by “forwards the error to the error message of the field” you mean automatically calling the .setErrorMessage(…) method on the field, but I checked and it does not work :frowning_with_open_mouth: The field is displayed in red, but no error messages…
Here’s what my attempt to emulate the .asRequired(…) method looked like:
binder .forField(surnameField) .withValidator(String::isBlank, "Surname must not be blank.") .withValidationStatusHandler(status -> status.getMessage().ifPresent(surnameField::setErrorMessage)) .bind(User::getSurname, User::setSurname);

Stop using withValidationStatusHandler

Ok :)) How then can I achieve what I want? :slightly_smiling_face:

Remove that line and it’s automatically done by the binder

binder .forField(surnameField) .withValidator(String::isBlank, "Surname must not be blank.") .bind(User::getSurname, User::setSurname);

Now there is nothing at all - no error message, no red highlight of the field, despite the fact that the field is empty… :frowning_with_open_mouth:

I think there is a problem with isBlank, if I remember correctly it has to be isNotBlank

Looking at the docs:

withValidator(
email → email.endsWith(“@acme.com”),
“Only acme.com email addresses are allowed”)

If the field DOES NOT ends with acme → validation fails and shows the error

Damn it worked! Thanks a lot!!!

:+1: