Search code examples
gwtbean-validationgwt-editors

GWT editors: show ConstraintViolations AND EditorErrors


I'm having a hard time to achieve that GWT editors show possible ConstraintViolations and EditorErrors at the same time. It's no problem to either show the errors or the validation violations.

The following example uses some number field of an Entity

Entity.java:

....
@NotNull
private Integer number;

public Integer getNumber() {
    return number;
}

public void setNumber(Integer number) {
    this.number = number;
}
...

EntityEditor.ui.xml

...
<editor:ValueBoxEditorDecorator ui:field="number">
    <editor:valuebox>
        <g:IntegerBox />
    </editor:valuebox>
</editor:ValueBoxEditorDecorator>
...

EntityEditor.java

...
@UiField
ValueBoxEditorDecorator<Integer> number;
private Validator fValidator;
private EntityEditorDriver fEditorDriver;
...
public void validate() {
    Entity entity = fEditorDriver.flush();
    Set<ConstraintViolation<Entity>> violations = fValidator.validate(entity);
    if (!violations.isEmpty() || fEditorDriver.hasErrors()) {
        fEditorDriver.setConstraintViolations(violations);
    } else {
        // process the entity
    }
}

When I call validate() without entering anything in the number box the message "Cannot be empty." is correctly displayed. Calling validate() with "asdf" I expect to see both messages "Cannot be empty." and "Bad value (asdf)", but only the first one is shown. The editor error is shown when I don't call setConstraintViolations() but obviously no validation violations will be shown.

What am I missing?

Thank you.


Solution

  • You'll have to getErrors() and merge them with your constraint violations (implementing a ConstraintViolation that wraps an EditorError)