Feras Odeh
Feras Odeh

Reputation: 9296

Validate user input in GWT?

What is the best way to validate user input in GWT? Is there any built in support for input validation? or is there any external framework to do that ? taking into consideration that I'm using hibernate with GWT?

Thanks

Upvotes: 0

Views: 3574

Answers (2)

Knubo
Knubo

Reputation: 8443

Since you haven't gotten a good answer, I want to share my opinion with the validation frameworks that I have seen for GWT.

The thing with the frameworks is that they often try to accomplish two things:

  • They want to be very general.
  • They want to be non intrusive to your code.

This will sometimes succeed, but most of the time they don't. Writing such a framework has a cost, and that cost is you as a user of that framework that will pay.

Validation should, in my opinion be simple. Here is an example on how I solved validation using some code I put together:

    MasterValidator masterValidator = new MasterValidator();

    masterValidator.mandatory(messages.required_field(), lastnameBox, firstnameBox, genderBox);

    if (birthdateRequired) {
        masterValidator.mandatory(messages.required_field(), birthdateBox);
    }

    masterValidator.date(messages.date_format(), birthdateBox);

    masterValidator.email(messages.invalid_email(), emailBox);

    return masterValidator.validateStatus();

Here each of the input boxes inherit from my TextField (or some other input types) and these fields has an error label that will be set if the validation is unsuccessful.

I'm not saying that this is perfect, but it will get the job done simply. If you want to be inspired/take some of my code for this, then please do. The code is used in a GPL2 licensed project hosted on Google Code:

http://code.google.com/p/accountclient/source/browse/#svn/trunk/RegnskapClient/src/no/knubo/accounting/client/validation

It uses the client/ui as well.

Upvotes: 0

Harald Schilly
Harald Schilly

Reputation: 1108

You can write static validation checker routines in a class stored in a "shared" package. Then, you can use that same code on the client and server side.

Upvotes: 1

Related Questions