Skip to content

BeanValidationBinder says the form is valid even though required (@NotNull) field is not filled #9000

@mstahv

Description

@mstahv

Vaadin 8.0.5

Example code to reproduce

public static class Entity {

    @NotNull
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Entity, name: " + name;
    }

}

final TextField name = new TextField();

@Override
protected void init(VaadinRequest vaadinRequest) {
    final VerticalLayout layout = new VerticalLayout();
    name.setCaption("Type your name here:");

    Entity entity = new Entity();
    Binder<Entity> b = new BeanValidationBinder<>(Entity.class);
    b.bindInstanceFields(this);

    b.setBean(entity);

    Button button = new Button("Check validity");
    button.addClickListener(e -> {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        Set<ConstraintViolation<Entity>> validate = validator.validate(entity);
        String msg = String.format("Binder.isValid: %s, JSR 303 valid: %s", b.isValid(), validate.isEmpty());
        Notification.show(msg);
    });

    layout.addComponents(name);
    layout.addComponent(button);

    setContent(layout);
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions