|
GWT_Validation_2_0
IntroductionEven though the GWT SDK will include (some or all) JSR-303 compatibility with version 2.3 or 2.4 it still requires (from what knowledge is available) a non-client check of the file for constraint violations. This is, at the very least, undesirable. Why?It is entirely possible to create a fully, or almost fully, complete JSR-303 validation solution, with GWT, that has the following features:
JSR-303 Support Progress (80%)Chapter 2: Constraint Definition (100%) 2.1: Constraint Annotation (100%) Chapter 3: Constraint declaration and validation process (75%) 3.1: Requirements on classes to be validated with Introspector (100%) Chapter 4: Validation APIs (100%) 4.1 Validator API (100%) Chapter 5: Constraint metadata request APIs (100%) 5.1: Validator (100%) InstructionsPlease refer to the dependencies wiki page first for all the other dependencies you will need to run the validation framework. You'll also need to add to your module xml file: <inherits name='com.em.validation.Validation' /> Please note, the path to the validation module file has changed To use this in your code you will need to build a validation factory and use it to obtain Validator objects. Any object that is on your classpath and marked with a constraint will have all the needed files generated. Here is a simple example of a constrained object: public class Person {
@NotNull
@Size(min=1,max=100) //could used composed NotEmpty and sacrifice length
private String firstName = null;
@NotNull
@Size(min=1,max=100)//could used composed NotEmpty and sacrifice length
private String lastName = null;
public getFirstName() {
return firstName;
}
public getLastName() {
return lastName;
}
/*
... snip setters and other methods ...
*/
}Notice that, unlike gwt-validation 1.0, there is no marker interface. The class will be found and have metadata generated at code generation time through the gwt deferred binding mechanism. Now you need to get the validator object from the validation factory import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.ConstraintViolation;
/* ... snip ... */
//get validator factory using default bootstrap mechanism of the Validation library
ValidatorFactory factory = Validation.byDefaultProvider().configure().buildValidatorFactory();
//get a validator instance
Validator validator = factory.getValidator();
//create new object
Person person = new Person();
person.setFirstName("Andrew");
//validate person object
Set<ConstraintViolation<Person>> violations = validator.validate(person);
//should be one violation from lastName being null
assert violations.size() == 1;It's that simple. No GWT.create() and no code differences between the client and the java code. None. Easy, fluent, and compliant. |
I am confused about which "inherit" module to use. Changing to " com.em.validation.client.validation.Validation" fails, as it is not found in the project sources or resources.
Sorry about that, it was incorrect here. It has been updated.
If I call factory.getValidator() I get java.lang.RuntimeException?: Deferred binding failed for 'javax.validation.ConstraintValidatorFactory?' (did you forget to inherit a required module?)
<inherits name='com.em.validation.Validation' /> is included in my Application.gwt.xml and validation-api-1.0.0.GA-sources.jar, gwt-2.4.0/validation-api-1.0.0.GA.jar is included in the class-path.
I have the same error, how can fix it?, it´s neccesary modify the inherits configuration??
Same error too.
Hi, I have just about everything working both on client and server sides, except for the Validation Messages... rather than getting a message from the properties file (ValidationMessages?.properties, which I put in the root of the project as instructed) I just get the key, for example "{javax.validation.constraints.NotNull?.message}". Looking into the ConstraintViolation? object, both getMessage() and getMessageTemplate() return the message key between curled brackets. Can anyone help please?
The problem with deferred binding failing above could be due to dependencies conflicts... as I have no such problem, perhaps the dependencies I included in my project (I'm using maven) may help solve that (I also had to remove the hibernate validator dependency which I had included previously which caused conflicts): <code language="xml"> <dependency> <groupId>com.em</groupId> <artifactId>gwt-validation</artifactId> <version>2.0-SNAPSHOT</version> </dependency> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.0.0.GA</version> </dependency> </code>
I'm pretty sure that's all you would need. As I am building my project using maven, I found that there's a problem with the pom.xml file that I pulled off the SVN repo which made it invalid, so none of the dependencies of the project would work... I simply commented out the dependency on javax.validation.validation-api-sources (which shouldn't have been there in the first place) and everything worked. Maybe this could be causing you guys trouble as well.
I also had that problem. The solution was that besides the jar that the google provides, you have to put on your CLASS_PATH the following jars: reflections-0.9.5.jar, guava-r09.jar, slf4j-api-1.6.1.jar, javassist-3.12.1.GA.jar, dom4j-1.6.1.jar, freemarker-2.3.18.jar, slf4j-simple-1.6.1.jar, xml-apis-1.0.b2.jar. The way that i did it, was also using maven to get this dependencies. I also have in GWT SDK the following jars: gwt-user.jar, gwt-dev.jar, validation-api-1.0.0.GA.jar
I'm not using DTOs, and directly pass the JPA annotated server side entities to the client side. I'm using gwt-validation v2 + GWT 2.4, but I don't get any constraint violations. This was working with gwt-validation v1 + GWT 2.2. Is this a known issue, or do I have an incorrect configuration?
are you getting any exceptions?
Seems to be some problems with old browsers (FF 3.6, FF 7, IE 7 and 8) which do not support the JavaScript? "console" object. Getting a JavaScript? exception when validation tries to write to Java script console log. Workaround was to create a null console object using native JavaScript? call.
Can someone Just upload the zip / 7zip file of a working project.
Hi, is it possible to user gwt validator even when someone is not using EditorsFramework?