Using Bean Validation in Vaadin 8.

I want using bean validation in vaadin 8.1

[code]

public class Person implements Serializable {
    @NotNull
    @javax.validation.constraints.Size(min=2, max=10)
    String name;
    
    @Size(min=2,max=10)
    String title;
    
    @Min(1)
    @Max(130)
    int age;

    
    public Person() {
    }

   /*..... Setter Getter .......*/
    
    
}

[/code]in example
i using this code

TextField textField = new TextField("Vaadin 8");
        Binder<Person> binder = new Binder<>();
        binder.setBean(person);
        binder.forField(textField)
            .withValidator(new StringLengthValidator("Too short", 8, 256)).bind(Person::getName,Person::setName);
            
        validationForm.addComponent(textField);

but it’s not work. my validation in Person class not work. what should i do?

Have you tried the easybinder add-on?
https://vaadin.com/directory#!addon/easybinder

-Olli

This should work:

TextField textField = new TextField("Vaadin 8");
        Binder<Person> binder = new BeanValidationBinder<>(); // Binder class does not add JSR validation, you should use BeanValidationBinder
        binder.setBean(person);
        binder.forField(textField)
            .withValidator(new StringLengthValidator("Too short", 8, 256)).bind("name"); // bind with person::getName ,Person::setName does not add JSR validation
            
        validationForm.addComponent(textField);

You should try this add-on (easybinder but i didn’t try it) if you want to use bean validation binder for a vaadin application because Vaadin bean validation binder is incomplete.

Yes i’ve try this add on. but i think is little dangerous to use add on with low maturity in my product.

“Vaadin bean validation binder is incomplete”. really? yes that code it’s work but my rule in class not work. example : if i type 14 character in text field, the text field not show the notification.

Vaadin bean validation has no cross field validation (for me this is the main problem), and don’t validate property if it’s bind with getter and setter. It can work with setBean but it “can’t work immediatly” with readBean/writeBean (for cross validation).

Problems are listed oin the Readme of the addon EasyBinder.

The validation is working or not ? I don’t understand (if the validation is not working then the code is not working :stuck_out_tongue: )