Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BindingBuilder.asRequired() returns true for nulls #11401

Closed
mvysny opened this issue Dec 30, 2018 · 6 comments
Closed

BindingBuilder.asRequired() returns true for nulls #11401

mvysny opened this issue Dec 30, 2018 · 6 comments
Labels
question workaround Workaround provided in ticket comments

Comments

@mvysny
Copy link
Member

mvysny commented Dec 30, 2018

Vaadin 8.6.3. I have the following binder setup:

        final Binder<Person> binder = new Binder<>(Person.class);
        final TextField name = new TextField();
        name.setCaption("Type your name here:");
        binder.forField(name).withNullRepresentation("").asRequired().bind("name");

The problem is that with this setup, an empty TextField passes the asRequired() validation and null is passed into Person.setName() with the following code:

if (binder.isValid() && binder.writeBeanIfValid(p)) { 
  // all ok
}
@mvysny
Copy link
Member Author

mvysny commented Dec 30, 2018

vaadin-app.zip
Attaching a project reproducing the issue.

@TatuLund
Copy link
Contributor

TatuLund commented Jan 9, 2019

Default implementation of asRequired() is a short hand for the following

See: https://github.com/vaadin/framework/blob/master/server/src/main/java/com/vaadin/data/Binder.java#L912

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

The method with custom asRequired validator is also public, see

https://vaadin.com/api/framework/8.6.3/com/vaadin/data/Binder.BindingBuilder.html#asRequired-com.vaadin.data.Validator-

Which gives a workaround

asRequired(Validator.from(value -> (value != null && !Objects.equals(value, field.getEmptyValue()))

@TatuLund TatuLund added the workaround Workaround provided in ticket comments label Jan 10, 2019
@mvysny
Copy link
Member Author

mvysny commented Feb 10, 2019

Thank you for posting the workaround. Yet I believe that nulls effectively mean "no value" and thus the standard asRequired() method should catch them in addition with the empty value, exactly as your workaround suggests.

@stale
Copy link

stale bot commented Jul 10, 2019

Hello there!

We are sorry that this issue hasn't progressed lately. We are prioritizing issues by severity and the number of customers we expect are experiencing this and haven't gotten around to fix this issue yet.

There are a couple of things you could help to get things rolling on this issue (this is an automated message, so expect that some of these are already in use):

  • Check if the issue is still valid for the latest version. There are dozens of duplicates in our issue tracker, so it is possible that the issue is already tackled. If it appears to be fixed, close the issue, otherwise report to the issue that it is still valid.
  • Provide more details how to reproduce the issue.
  • Explain why it is important to get this issue fixed and politely draw others attention to it e.g. via the forum or social media.
  • Add a reduced test case about the issue, so it is easier for somebody to start working on a solution.
  • Try fixing the issue yourself and create a pull request that contains the test case and/or a fix for it. Handling the pull requests is the top priority for the core team.
  • If the issue is clearly a bug, use the Warranty in your Vaadin subscription to raise its priority.

Thanks again for your contributions! Even though we haven't been able to get this issue fixed, we hope you to report your findings and enhancement ideas in the future too!

@stale stale bot added the Stale Stale bot label label Jul 10, 2019
@stale stale bot removed the Stale Stale bot label label Nov 25, 2019
@Legioth
Copy link
Member

Legioth commented Nov 26, 2019

I think there's an even easier workaround, but I'm too lazy to test it in practice. The null representation converter and the required validator are forming a processing chain where order matters. If you change the order between validator and the converter, then the validator will see the original value from the field (i.e. "") rather than null and thus fail validation.

binder.forField(name).asRequired().withNullRepresentation("").bind("name");

@NetRob
Copy link

NetRob commented Mar 1, 2020

I think there's an even easier workaround, but I'm too lazy to test it in practice. The null representation converter and the required validator are forming a processing chain where order matters. If you change the order between validator and the converter, then the validator will see the original value from the field (i.e. "") rather than null and thus fail validation.

binder.forField(name).asRequired().withNullRepresentation("").bind("name");

setting asRequired before withNullRepresentation actually solves it for me 👍
(tested in Vaadin 14 with an required Long value)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question workaround Workaround provided in ticket comments
Projects
None yet
Development

No branches or pull requests

4 participants