How to install gwt-validation with gwt-2.4.0
Asked Answered
D

2

1

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.

Diamine answered 26/1, 2012 at 9:58 Comment(0)
D
1

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).

Diamine answered 27/1, 2012 at 9:0 Comment(1)
This is a pretty common problem. I did that to myself the other day when adding a new unit test.Aikoail
R
0

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!

Riannon answered 27/1, 2012 at 8:56 Comment(2)
Thanks for your answer but I'm not using Hibernate.Diamine
You dont have to use hibernate for this, I am not using Hibernate either. But hibernate-validator is the default provider for validation on the server side.Riannon

© 2022 - 2024 — McMap. All rights reserved.