How to implement validation

64 views
Skip to first unread message

nikola...@gmail.com

unread,
Nov 15, 2006, 5:00:16 PM11/15/06
to Google Web Toolkit
Well, playing around with GWT has led to some design questions.

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

Eric

unread,
Nov 16, 2006, 9:29:57 AM11/16/06
to Google Web Toolkit
We implemented a form validation framework on the client, and it works
quite nicely with the i18n facility. I looked at solving the "same
validator on the server as on the client", but I gave up. There were
two reasons: 1) client-side validation has different requirements than
validating business service inputs, and 2) repeating the same
validation on the server-side is not that important for our apps (and
unless you're writing an app where this type of lock-down security is
required, why do it?).

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

nikola...@gmail.com

unread,
Nov 16, 2006, 2:24:29 PM11/16/06
to Google Web Toolkit
Thx for your input, 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.

nikola...@gmail.com

unread,
Nov 16, 2006, 2:42:53 PM11/16/06
to Google Web Toolkit
I remember this validator style from Cocoon.

GWT doesn't provide validation facilities, does it ? Did you implement
wrappers around your widgets ?

Reply all
Reply to author
Forward
0 new messages