如何使用GWT编辑器框架进行验证?

Jan*_*Jan 19 java gwt editor uibinder gwt2

我正在尝试与GWT 2.1.0的新GWT Editor框架集成.我还想将验证检查添加到框架中.但是,我正在努力寻找一个体面的例子来做这件事.

目前我有以下代码:

<!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> 
Run Code Online (Sandbox Code Playgroud)

对于我的编辑:

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));
    }

}
Run Code Online (Sandbox Code Playgroud)

在什么时候我必须调用我的验证器,我该如何与它集成?

更新:

我实际上正在寻找一种方法来检索具有关键属性路径的地图,以及编辑器的值.委托上有一个路径字段,但这不是编辑对象中的路径,而是编辑器类中的路径.

有人知道是否有可能做这样的事情?

Ant*_*nio 8

使用contstrants注释bean(请参阅Person.java)

public class Person {
  @Size(min = 4)
  private String name;
}
Run Code Online (Sandbox Code Playgroud)

使用标准验证引导程序在客户端上获取Validator并验证您的对象(请参阅ValidationView.java)

Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<Person>> violations = validator.validate(person);
Run Code Online (Sandbox Code Playgroud)

按照此模式为要在客户端上验证的对象创建验证器.(参见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);
  }
}
Run Code Online (Sandbox Code Playgroud)

包括验证提供程序的模块.在gwt模块文件中添加replace-with标记,告诉GWT使用您刚刚定义的Validator(请参阅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>
Run Code Online (Sandbox Code Playgroud)

资源

  • 太糟糕了,这还没有绑定到编辑器框架,以提供输入字段旁边的验证消息. (2认同)