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.
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
Thanks Pleku, good ideas.
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.
@Thibstars, you can use standard Vaadin 8 validation API for that. Use Binder.Binding.validate() for that.
@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.
@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?
@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).
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.
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();
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):
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!
This feature has been implemented as add-on in Directory https://vaadin.com/directory/component/fieldbinder
Most helpful comment
Another way to do that is to have an additional method to Binder.
For instance, for now, I have to do that:
But ideally a noBind() method can be added: