Search code examples
javagwteditoruibindergwt2

How to use the GWT editor framework for validation?


I am trying to integrate with the new GWT Editor framework of GWT 2.1.0. I also want to add my validation checks into the framework. However, I am struggling to find a decent example how to do this.

For the moment I have the following code:

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
    xmlns:g="urn:import:com.google.gwt.user.client.ui" xmlns:e="urn:import:com.google.gwt.editor.ui.client">
    <ui:with type="be.credoc.iov.webapp.client.MessageConstants"
        field="msg" />
    <g:HTMLPanel>
        <e:ValueBoxEditorDecorator ui:field="personalReference">
            <e:valuebox>
                <g:TextBox />
            </e:valuebox>
        </e:ValueBoxEditorDecorator>
    </g:HTMLPanel>
</ui:UiBinder> 

And for my editor:

public class GarageEditor extends Composite implements Editor<Garage> {

    @UiField
    ValueBoxEditorDecorator<String> personalReference;

    interface GarageEditorUiBinder extends UiBinder<Widget, GarageEditor> {
    }

    private static GarageEditorUiBinder uiBinder = GWT.create(GarageEditorUiBinder.class);

    public GarageEditor() {
        initWidget(uiBinder.createAndBindUi(this));
    }

}

On what point do I have to call my validator and how do I integrate with it?

Update:

I am actually looking for a way to retrieve a map with as key the property path, and as value the editor. There is a path field on a delegate, but this is not the path within the edited object, but the path in the editor class.

Does anybody know if it is possible to do something like this?


Solution

  • Annotate you beans with contstrants (see Person.java)

    public class Person {
      @Size(min = 4)
      private String name;
    }
    

    Use the standard validation bootstrap to get a Validator on the client and validate your object (see ValidationView.java)

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

    Follow this pattern to create a Validator for the objects you want to validate on the client. (see SampleValidatorFactory.java)

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

    Include the module for your Validation Provider. Add replace-with tag in your gwt modle file telling GWT to use the Validator you just defined (see Validation.gwt.xml)

    <inherits name="org.hibernate.validator.HibernateValidator" />
    <replace-with
        class="com.google.gwt.sample.validation.client.SampleValidatorFactory">
        <when-type-is class="javax.validation.ValidatorFactory" />
    </replace-with>
    

    Source