How to use BeanValidationBinder ?

Hi,

I’m trying to understand how I can use BeanValidationBinder to validate my bean.

I can validate properties but I can’t validate bean level contraints (not done by BeanValidationBinder if I understand it correctly).
I create a small project to test it and I don’t like the result :slight_smile: ( = I won’t use it in a project)
https://github.com/jcgueriaud/vaadin-example-bean-validation-jsr

There are some opened issues on BeanValidationBinder, especially:

Did someone use JSR Validation with Vaadin 8 ?

Thanks,

There is currently an issue with the @NotNull annotation.
https://github.com/vaadin/framework/issues/9000

Other validation annotation such as @Size and @Pattern works for me.

I didn’t try @Pattern but it should work because BeanValidator validates value for the bound property (like size …).
The main problem is for cross field validation (bean-level validation) because it’s not implemented :confused:

I found something strange about bean level validation. Vaadin documentation on Data Binding, says:


Bean level validation can only be performed once the bean has been updated. This means that this functionality can only be used together with setBean. You need to trigger validation manually if using readBean and writeBean.

So I can’t add validation to a binder object if I use read/write approach, but only using setBean(). I think this is an asimmetric solutions and not so good.
The only way out is to manually test validations on value changed event or something like this?

With the last version of Vaadin:

  • when you change a value it validates the value (if setBean and readBean/writeBean) =
    Set<? extends ConstraintViolation<?>> violations = getJavaxBeanValidator().validateValue(beanType, propertyName, value);
    is called.
    This is why it can be immediate in both mode and why you can link the error message to a specific Field.

To validate a bean level constraints, you need the updated bean, in unbuffered mode you got the updated bean so it’s possible to call the bean validation when you change one field.
For buffered mode, the bean level validation can be done when you call writeBean, because you don’t have the bean in the binder before. So the validation error is thrown when writeBean.

So for me the documentation is wrong. You can use it with setBean and readBean but the bean level error is displayed on writeBean. (when bean is updated)

Hi Jean
I tryed to set a validator on binder, (i.e. binder.withValidator()) and use read/write bean. Framework give me error “bean is not set”. If I use binder.setBean() it works.

I’ve just update my test project on github with readBean but I use Vaadin 8.1.0.beta1. The bean validation is triggered.
Didn’t try on Vaadin 8.0.

the bean validation doesn’t work; Im using vaadin 8.1.0