GWT JSR 303 client validation

后端 未结 3 1667
后悔当初
后悔当初 2020-12-29 10:51

I\'m developing a GWT application with a Spring backend that uses JSR 303 validation. The application can go offline and use the browser HTML5/Gears database instead.

<
3条回答
  •  心在旅途
    2020-12-29 11:13

    GWT 2.5 has a new feature just for this : https://developers.google.com/web-toolkit/doc/latest/DevGuideValidation

    It uses the Hibernate Validator.

    1) You will need to extend AbstractGwtValidatorFactory and apply your bean e.g. :

    public final class SampleValidatorFactory extends AbstractGwtValidatorFactory {
    
      /**
       * Validator marker for the Validation Sample project. Only the classes and groups listed
       * in the {@link GwtValidation} annotation can be validated.
       */
      @GwtValidation(Person.class)
      public interface GwtValidator extends Validator {
      }
    
      @Override
      public AbstractGwtValidator createValidator() {
        return GWT.create(GwtValidator.class);
      }
    }
    

    2) Then add this to your gwt.xml :

    
    
      
    
    

    3) Validate your bean

    Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
    Set> violations = validator.validate(person);
    

提交回复
热议问题