You could use GWT Bean Validation which is part of GWT 2.4 and works fine, although it is still in an experimental state (see http://code.google.com/p/google-web-toolkit/wiki/BeanValidation). GWT Bean Validation supports JSR303 annotations. This means that you can define constraints on your Data Transfer Objects (DTO). We use it together with GWT RequestFactory and the GWT Editor Framework.
Now, if you want to change the style of a widget you can implement something similar to ValueBoxEditorDecorator
. Here is an example of how you can write such a decorator for your widget and add a css class (like Alex suggested):
public class MyWidgetDecorator<T> extends Composite implements
HasEditorErrors<T>, IsEditor<ValueBoxEditor<T>> {
// ...
@UiField
SimplePanel container;
@UiField
DivElement errorLabel;
// ...
/**
* Sets the widget that is to be decorated.
*/
@UiChild(limit = 1, tagname = "content")
public void setContent(ValueBoxBase<T> widget) {
container.add(widget);
// ... if using the editor framework, initialize the widget as editor
}
public void showErrors(List<EditorError> errors) {
StringBuilder sb = new StringBuilder();
for (EditorError error : errors) {
if (error.getEditor().equals(editor)) {
sb.append("\n").append(error.getMessage());
}
}
if (sb.length() == 0) {
errorLabel.setInnerText("");
errorLabel.getStyle().setDisplay(Display.NONE);
if (container.getWidget() != null) {
container.getWidget().removeStyleName("violation"); // remove css class on widget
}
return;
}
errorLabel.setInnerText(sb.substring(1));
errorLabel.getStyle().setDisplay(Display.INLINE_BLOCK);
if (container.getWidget() != null) {
container.getWidget().addStyleName("violation"); // add css class on widget
}
}
}
In the method showErrors(...)
a css class called "validation" is added to the widget (e.g. a text box). In the css class you could then define a red border for the widget to illustrate that the input is invalid, for example.
By the way, a good explanation on how to use the GWT Editor Framework can be found in this answer.