If I use a Form and the validation fails, ONE error message is displayed at the bottom of all fields.
Is it possible to display all validation failures of all form fields, or only just one?
If I handle validation error manually by catching the validation exception after validate() or commit() and calling setComponentError(UserError), the error indicator is shown and the error message in a tooltip.
But how can I reach the same behaviour as the Form, i.e. displaying the error message as a (red) label? Do I have to implement this manually? Although having debugged the Form I just do not understand how the Form handles this.
Sometimes I got a strange tooltip like that “<1>error<2>object:love:>my user error message” if I call setComponentError(UserError). I expected to see just the “my user error message”. Any idea what I’m doing false?
Sorry for my bad english this time, but it’s late and I’m tired.
On commit the form displays the first validation error message at the end of the form. Additionally it adds error indicators to all fields that fails validation (hovering the field shows the errors). In many cases it may be annoying for the user if all errors are listed in two places.
To override the behavior you have several options. The first is something like:
final Form frm = new Form();
frm.setValidationVisible(false);
frm.setValidationVisibleOnCommit(false);
TextField a = new TextField("Field a", "value 1");
TextField b = new TextField("Field b", "value 2");
frm.addField("a", a);
frm.addField("b", b);
a.addValidator(new StringLengthValidator("Field must be 10-20 chars",
10, 20, false));
b.addValidator(new StringLengthValidator("Field must be 10-20 chars",
10, 20, false));
Button but = new Button("submit", new ClickListener() {
public void buttonClick(ClickEvent event) {
try {
frm.commit();
} catch (Exception e) {
frm
.setComponentError(new UserError(
"This is a custom error messages. Something failed."));
}
}
});
The other option would be to override the Form.getComponentError() method and return whatever you like to display as the error message. E.g.
final Form frm = new Form() {
@Override
public ErrorMessage getComponentError() {
return new UserError("Validation failed!");
}
};
will always write “Validation failed!” below the form (even if there is no failure).