I have the following project setup:
1) GWT client, contains the client mode (IsSerializable), only GWT
compatible code, no 3rd-party libs etc.
2) GWT facade layer, hosted on the web tier, serves as a facade between
the server side web tier and the GWT client (i.e. it converts between
the server side model and the client side model, of course it must
validate all client input again)
3) Core layer, includes the server side model (java.lang.Serializable)
and the business logic (Spring)
I tried to implement validators for my domain objects, but with GWT and
i18n for the error messages this leads to some problems:
1) With GWT i18n is implemented by binding method names to resource
bundle entries. If my validator classes provide error message keys (as
one would usually implement this) then i'd have to use reflection to
get the localized error messages. A bad idea because i'd loose compiler
checks for the message parameters. This is just too risky for a serious
application.
2) If i call the generated i18n-methods directly when instantiating the
validation exceptions then I wouldn't be able to use the validators in
my GWT facade layer, because GWT simply doesn't run there and thus it
can't use any GWT generated i18n code.
As far as I can see I'll have to implement 3 different versions of
every validator:
1) GWT validator, direct calls to the GWT generated i18n classes, runs
on the client
2) GWT validator, uses the resource bundles with file access, runs in
the GWT facade layer
3) Server side validator for the server side model
3 classes for the same purpose, just because GWT is unable to use
java.lang.Serializable and generates proprietary i18n code.
I'd like to suggest the following enhancements:
1) Support for java.lang.Serializable
2) A i18n class that can use message constants for lookup and not
generated Java methods
IMO, the functional requirements for client-side and server-side
validation are just too different to stuff them into a single
implementation. For example, our form validation framework hilights
fields that don't validate, so that the user knows exactly what they
have to fix. The server-side (business layer) knows nothing about form
fields (it shouldn't... the dependency goes the other way). For a
business service, input validation should just check the rules, and if
anything is invalid, the service should raise an exception. Even when
we were using Struts, client-side validation was different than
service-layer input validation.
By way of example, here is a snippet from one of our client validators
(with i18n):
public static final ValidationRule REQUIRED = new ValidationRule() {
public ValidationError validate(FormField field) {
if (field.getEditWidget() instanceof HasText) {
String text = field.getWidgetText();
if (text.equals("")) {
return new ValidationError(field,
Core.messages.getFormFieldRequired(field.getLabelText())); // i18n
message
}
}
return null;
}
};
-eric
So your client validation framework can be used to attach validation
rules to individual widgets. Thats a nice approach and I'll give it a
try.
GWT doesn't provide validation facilities, does it ? Did you implement
wrappers around your widgets ?