How to install gwt-validation with gwt-2.4.0
-
20-04-2021 - |
Question
I would like to use Gwt-Validation with Gwt 2.4.0.
I have in my gwt.xml file :
<inherits name="javax.validation.Validation" />
<inherits name="com.google.gwt.validation.Validation" />
<inherits name='com.em.validation.Validation' />
Like in the documentation, I have the following code :
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
...
ValidatorFactory factory =
Validation.byDefaultProvider().configure().buildValidatorFactory();
Validator validator = factory.getValidator();
The problem is that I have the following error :
Deferred binding result type 'javax.validation.ValidationFactory' should not be abstract.
//... beautiful stacktrace with mainly :
Deferred binding failed for 'javax.validation.ValidatorFactory' (did you forget to inherit a required module?)
My pom.xml contains :
<dependency>
<groupId>com.googlecode.gwt-validation</groupId>
<artifactId>gwt-validation</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
<classifier>sources</classifier>
<scope>provided</scope>
</dependency>
What can I do ? Thanks.
Solution
The problem was in the java imports.
In the following code :
ValidatorFactory factory =
Validation.byDefaultProvider().configure().buildValidatorFactory();
Validator validator = factory.getValidator();
I imported :
com.google.gwt.validation.client
instead of javax.validation
No error at java compile time ! Just the errors above at gwt compile time (or runtime with the hosted mode).
OTHER TIPS
I have this in my GWT.XML file :
<inherits name="org.hibernate.validator.HibernateValidator" />
<replace-with class="com.example.client.MyValidatorFactory">
<when-type-is class="javax.validation.ValidatorFactory" />
</replace-with>
I defined MyValidatorFactory like this :
public final class MyValidatorFactory extends AbstractGwtValidatorFactory {
/**
* Validator marker for the Validation Sample project. Only the classes
* listed in the {@link GwtValidation} annotation can be validated.
*/
@GwtValidation(value = { Customer.class, License.class, Account.class })
public interface MyValidator extends Validator {
}
@Override
public AbstractGwtValidator createValidator() {
return GWT.create(GwtValidator.class);
}
}
In my classpath I have :
hibernate-validator-4.2.0.Final-sources.jar
hibernate-validator-4.2.0.Final.jar
Now you can validate your domain objects :
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<Account>> violations = validator.validate(account);
And the same validation then also works server-side :
private static ValidatorFactory factory = Validation.byDefaultProvider().configure().buildValidatorFactory();
Set<ConstraintViolation<DomainResource>> violations = factory.getValidator().validate(account);
Good stuff!