Question
I have problem with using Hibernate Validator with GWT 2.4. When module is starting i get error:
00:00:05,562 [ERROR] Deferred binding result type 'javax.validation.ValidatorFactory' should not be abstract
java.lang.RuntimeException: Deferred binding failed for 'javax.validation.ValidatorFactory' (did you forget to inherit a required module?) ...
module configuration:
<module rename-to='start'>
...
<inherits name="com.google.gwt.validation.Validation" />
<!-- with this doesn't work too
<inherits name="org.hibernate.validator.HibernateValidator" />
-->
...
</module>
entry point:
public class Start implements EntryPoint {
public void onModuleLoad() {
final ValidatorFactory factory = Validation.byDefaultProvider().configure().buildValidatorFactory();
...
}
}
pom.xml
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
Solution
SOLVED this!
According to http://code.google.com/p/google-web-toolkit/wiki/BeanValidation i had to implement custom MyValidatorFactory
and add to project hibernate-validator sources .jar.
GWT module configuration:
<!-- hibernate validator -->
<inherits name="org.hibernate.validator.HibernateValidator" />
<replace-with class="net.marioosh.gwt.client.MyValidatorFactory">
<when-type-is class="javax.validation.ValidatorFactory" />
</replace-with>
MyValidatorFactory
package net.marioosh.gwt.client;
import javax.validation.Validator;
import net.marioosh.gwt.shared.model.entities.Link;
import com.google.gwt.core.client.GWT;
import com.google.gwt.validation.client.AbstractGwtValidatorFactory;
import com.google.gwt.validation.client.GwtValidation;
import com.google.gwt.validation.client.impl.AbstractGwtValidator;
public class MyValidatorFactory extends AbstractGwtValidatorFactory {
/**
* Only the classes listed in the {@link GwtValidation} annotation can be validated.
* In my example: Link.class can be validated
*/
@GwtValidation(value = { Link.class })
public interface GwtValidator extends Validator {
}
@Override
public AbstractGwtValidator createValidator() {
return GWT.create(GwtValidator.class);
}
}
Hibernate validator sources are needed also
<!-- local dependency -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-sources</artifactId>
<version>4.2.0.Final</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/hibernate-validator-4.2.0.Final-sources.jar</systemPath>
</dependency>
Using:
public void onModuleLoad() {
final ValidatorFactory factory = Validation.byDefaultProvider().configure().buildValidatorFactory();
final Validator validator = factory.getValidator();
...
Link l = new Link(....);
Set<ConstraintViolation<Link>> violations = validator.validate(l);
if(!violations.isEmpty()) {
// validation fail
} else {
// validation success
}
...
}
OTHER TIPS
Last time I tried to make this work, I just had a huge headache and decided to implement the GWT-validation framework myself, based on the JSR303 standard.
Anyway, this is still an experimental framework, as stated in the Wiki:
http://code.google.com/p/google-web-toolkit/wiki/BeanValidation
WARNING EXPERIMENTAL. The API may change. SOME things still just don't work.
So, if I were you, I wouldn't try to make it work. Implement yourself some generator that is capable of validating your classes.
I also tried this framework here:
http://code.google.com/p/gwt-validation/
... but again, totally failed to make it work for me! But depending on your cases, it might work fine... try to make a "very" small application to test the different approaches and see what works for you.