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

When I have just one field on the UI, I want to easily validate the value of the field, without having to use binder or listening to event and manually show error message #8294

Open
fakeh opened this issue Jan 20, 2017 · 11 comments
Labels
enhancement workaround Workaround provided in ticket comments

Comments

@fakeh
Copy link

fakeh commented Jan 20, 2017

Hi,
I believe this is the right place to leave V8 feedback.

Shouldn't it be possible to validate/convert values in Fields without having to bind to a bean?

I have a form with a single field. It seems weird to have to bind this one field to a bean in order to validate it.

Thanks!
Dan.

@pleku pleku added the question label Mar 2, 2017
@pleku
Copy link
Contributor

pleku commented Mar 2, 2017

tldr: We could make it possible to create a Binding for a field component with validators/converters without a Binder.

This is something that was left out of 8.0, you just basically should use a ValueChangeListener and trigger your validator/converter manually, setting and clearing the error message...

            TextField tf = new TextField();
            tf.addValueChangeListener(event -> {
                if (event.getValue().isEmpty()) {
                    tf.setComponentError(new UserError("Empty value not allowed"));
                } else {
                    tf.setComponentError(null);
                    Notification.show(event.getValue());
                }
            });

You can use Binder too without a bean but it requires some boilerplate

        TextField textField = new TextField();
        Binder<String> binder = new Binder<String>();
        Binding<String, String> binding = binder.forField(textField)
                .withValidator((value, context) -> value.isEmpty()
                        ? ValidationResult.error("Empty value not allowed")
                        : ValidationResult.ok())
                .bind(string -> string, this::onValueChange);
        binder.setBean("Initial Value"); // goes to textfield directly

@pleku pleku added candidate and removed question labels Mar 2, 2017
@fakeh
Copy link
Author

fakeh commented Mar 6, 2017

Thanks Pleku, good ideas.

@pleku pleku removed the candidate label Mar 22, 2017
@Thibstars
Copy link

What I seem to miss in this approach is the ability to check wether a field is valid. Something like textField.isValid(); would be great. This could come in handy when you have a button you only want to enable when all fields are valid, for example.

@elmot
Copy link
Contributor

elmot commented Mar 23, 2017

@Thibstars, you can use standard Vaadin 8 validation API for that. Use Binder.Binding.validate() for that.

@Thibstars
Copy link

@elmot Thanks for the suggestion. However I do still think that produces lengthy code as well. It works for me and I do think it is better than just use valuechangelisteners.

@fgrazi
Copy link

fgrazi commented May 2, 2017

@pleku Thank you very much, but I am unable to reproduce the snippet. In:
.bind(string -> string, this::onValueChange);
What is the value of this? The compiler complains that there is no onValueChange method. Can you please explain?

@pleku pleku changed the title Vaadin 8 Binding/Validation Feedback When I have just one field on the UI, I want to easily validate the value of the field, without having to use binder or listening to event and manually showing error message May 5, 2017
@pleku
Copy link
Contributor

pleku commented May 5, 2017

@fgrazi that is just an example, you should replace the this::onValueChange with whatever you want to do with the updated value from the field (after it has passed the validation).

In the example it is just something that consumes a String, which is the value type of the Binder (and TextField).

@pleku pleku changed the title When I have just one field on the UI, I want to easily validate the value of the field, without having to use binder or listening to event and manually showing error message When I have just one field on the UI, I want to easily validate the value of the field, without having to use binder or listening to event and manually show error message May 5, 2017
@fgrazi
Copy link

fgrazi commented May 5, 2017

Thanks pleku, but I still don't understand. Once I have a Binder.BindingBuilder
(resulting from forField call) I shall finally bind it in order to enable
the binder. But from javadoc I only see two bind methods: the first receives
the property name (not the case), the second a getter and a setter (unusable
since I have no bean), so how can I call the bind function? Note: also tried
a DynaBean but apparently it does not work.

@pinonpierre
Copy link

Another way to do that is to have an additional method to Binder.

For instance, for now, I have to do that:

Binder binder = new Binder();
binder.forField(textField)
   .asRequired("Required")
   .withValidator(new StringLengthValidator("Length Error", 3, 5 ))
   .bind(o -> textField.getEmptyValue(), null);

But ideally a noBind() method can be added:

Binder binder = new Binder();
binder.forField(textField)
   .asRequired("Required")
   .withValidator(new StringLengthValidator("Length Error", 3, 5 ))
   .noBind();

@stale
Copy link

stale bot commented Mar 19, 2018

Hello there!

It looks like this issue hasn't progressed lately. There are so many issues that we just can't deal them all within a reasonable timeframe.

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 Mar 19, 2018
@TatuLund
Copy link
Contributor

This feature has been implemented as add-on in Directory https://vaadin.com/directory/component/fieldbinder

@stale stale bot removed the Stale Stale bot label label Oct 19, 2019
@TatuLund TatuLund added enhancement workaround Workaround provided in ticket comments labels Oct 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement workaround Workaround provided in ticket comments
Projects
None yet
Development

No branches or pull requests

7 participants