带有编辑器框架的 GWT 验证器

发布于 2024-11-06 23:05:12 字数 1328 浏览 1 评论 0原文

有没有人意识到编辑器和 jsr 303 验证如何与 GWT 2.3 一起工作 未来?验证 API 已添加到 gwt sdk。但我无法使用编辑器框架验证实体。无论 我确实从来没有从客户端或服务器端抛出错误。

这是一个代码片段:

public class P {

  public P() {}

  @Size(min=4)
  private String name;

  public void setName(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }

}

PEditor

public class PEditor extends Composite implements Editor<P> {

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

  interface PEditorUiBinder extends UiBinder<Widget, PEditor> {}

  @UiField
  TextBox name;

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

}

  PEditor pEditor;
  interface Driver extends SimpleBeanEditorDriver<P, PEditor> {}

  Driver driver = GWT.<Driver> create(Driver.class);

  public void onModuleLoad() {

    pEditor = new PEditor();
    driver.initialize(pEditor);
    P p = new P();
    driver.edit(p);
    pEditor.name.setText("G");
    driver.flush();

    if(driver.hasErrors()) {
        List<EditorError> errors = driver.getErrors();

        for (EditorError error : errors) {
          System.out.println(error.getMessage());

        }

    }
  }

感谢您的帮助

Have anybody realized how editors and jsr 303 validation work with GWT 2.3
coming? Validation API was add to gwt sdk. But I am unable to validate entities using the editor framework. No matter what
I do the errors are never thrown from client side or server side.

Here is a code snippet:

public class P {

  public P() {}

  @Size(min=4)
  private String name;

  public void setName(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }

}

PEditor

public class PEditor extends Composite implements Editor<P> {

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

  interface PEditorUiBinder extends UiBinder<Widget, PEditor> {}

  @UiField
  TextBox name;

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

}

  PEditor pEditor;
  interface Driver extends SimpleBeanEditorDriver<P, PEditor> {}

  Driver driver = GWT.<Driver> create(Driver.class);

  public void onModuleLoad() {

    pEditor = new PEditor();
    driver.initialize(pEditor);
    P p = new P();
    driver.edit(p);
    pEditor.name.setText("G");
    driver.flush();

    if(driver.hasErrors()) {
        List<EditorError> errors = driver.getErrors();

        for (EditorError error : errors) {
          System.out.println(error.getMessage());

        }

    }
  }

Thanks for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜夜流光相皎洁 2024-11-13 23:05:12

验证 API,至少从 2.3 开始,不会为您构建客户端代码 - 它是一个可以集成在服务器上的工具,使您的服务器在某些情况下返回错误。

调用 EditorDriver.hasErrors() 只是检查是否有任何代码告诉本地委托是否有错误 - 可以通过此实现客户端验证。

现在最自动的情况是使用 RequestFactory 时 - 如果您的服务器类路径上有 javax.validation jar(api 和源)以及验证库(hibernate-validator 和 apache 的 bval 是两个这样的库),则接收器回调将调用 onViolation

使用 RequestFactory 从服务器获取违规行为后,可以使用 RequestFactoryEditorDriver 将错误推送到 UI,尽管使用 HasEditorErrors 编辑器实例和包装器,例如 < code>ValueBoxEditorDecorator,或者只是在 onViolation 发生时通过某种机制(警报、横幅、调试 sys.out.println 等)通知用户叫。

如果使用 RPC,您可以自行运行服务器验证,并(从 2.3 开始)使用验证过程中在服务器上生成的 ConstraintViolation 对象调用 driver.setConstraintViolations

The Validation API, at least as of 2.3, doesn't build client side code for you – it is a tool that can be integrated on the server to make your server spit back errors in certain cases.

The call to EditorDriver.hasErrors() is just to check if any code has told the local delegates if there are errors – client-side validation can be implemented through this.

The most automatic case right now is when using RequestFactory - if you have the javax.validation jar (both api and sources) on your server classpath as well as a validation library (hibernate-validator and apache's bval are two such libraries), the Receiver callback will have onViolation called.

With RequestFactory being used to get violations from the server, the RequestFactoryEditorDriver can then be used to push errors to the UI, though the use of HasEditorErrors editor instances and wrappers, like ValueBoxEditorDecorator, or just by notifying the user through some mechanism (alert, banner, your debug sys.out.println, etc) when onViolation is called.

If using RPC, you can run the server validations on your own, and (as of 2.3) call driver.setConstraintViolations with the ConstraintViolation objects generated on the server from the validation process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文