2

I have been researching on the better framework I could use to validate the data at client and server side. I know it is important to do validations at both the side.

I had thus come across something called GWT Validation Framework which can do validations at both the side. I have few JSP's. I have o validate the data filled in by user, at client side. But I haven't found a single example on how to do it? Can anyone please enlighten on the same.

Thank you

P.S: It would be grateful if someone could assist on some better client side validation methods(other than java script).

Anuj Balan
  • 7,629
  • 23
  • 58
  • 92

2 Answers2

2

GWT has support for compiling javax.validation into a compile module, but it isn't going to be easy to use without actually using GWT. The validation mechanism is powered by JSR-303 bean validations, and so needs to see the bean on both the client and on the server - a html client page created by a jsp isn't enough, you need to create and load a GWT module onto the page.

In GWT, you write what looks like Java, and it compiles to JavaScript. JSR303 support also gets compiled to javascript, so any amount of client side validation isn't enough - see Why is client-side validation not enough? for more explanation on that - your server also needs to run the validation.

If you are not already using GWT, then GWT's validation isn't going to make a lot of sense for your project. If you decide this all makes sense for you, then start using it - check out http://www.gwtproject.org/doc/latest/DevGuideValidation.html for more information and the sample project at https://github.com/gwtproject/gwt/tree/master/samples/validation for some source.

Community
  • 1
  • 1
Colin Alworth
  • 17,801
  • 2
  • 26
  • 39
  • Got it. So this validation framework is effective when used with GWT components. Can you suggest anyother good and latest client side validation techq's other than JS and Struts validation ? – Anuj Balan Feb 14 '12 at 04:39
  • Client side? How can it run on the client without using JS? Even struts or the like will use some server code to write out JS to the page to validate it - and I am guessing you have a finished JSP that you want to add this validation to, not to start from a new framework (wicket, tapestry, play, but there are lots of others) that support validation. – Colin Alworth Feb 14 '12 at 04:47
  • AS you guessed correctly. I have JSP's and would like to add client side validations. Using JS for this is a traditional way checking client side validations. Thus, I was checking if there are any more effective and modern way of doing it. – Anuj Balan Feb 14 '12 at 05:10
1
  1. For client side data validation

I am using Putnami Web Toolkit (PWT). This framework is compliant with commons JSR-303 bean validation annotations.

You can find documentation and live example at this location : http://pwt.putnami.org/#!Validation

  1. For server side data validation

I am using Hibernate's Bean Validation JSR-303 reference implementation ( version 4.3.2-Final ).

An example below :

imports :

import java.util.HashSet;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validation;
import javax.validation.ValidationException;
import javax.validation.Validator;

code :

final Set<ConstraintViolation<BeanToValidate>> violations = validator.validate(form);
        if (!violations.isEmpty()) {
            final Set<ConstraintViolation<?>> constraintViolations = new HashSet<ConstraintViolation<?>>(
                    violations);
            throw new ConstraintViolationException(constraintViolations);
        }
Stéphane B.
  • 3,260
  • 2
  • 30
  • 35