GWT验证可以改变该领域的风格

时间:2012-01-26 08:20:03

标签: gwt validation

我正在为GWT寻找能够在出错时更改小部件样式的验证库。

  1. 我没有在GWT-Validation中找到它(也许我错过了一些>。
  2. 我找到了具有GWT-VL功能的框架(http://gwt-vl.sourceforge.net),但是从2009年开始它已经死了(纯文档,没有maven repo工作)。
  3. 对于具有该功能的GWT,还有更多可用的验证框架吗?

2 个答案:

答案 0 :(得分:1)

你可以使用GWT Bean验证,它是GWT 2.4的一部分并且工作正常,尽管它仍处于实验状态(见http://code.google.com/p/google-web-toolkit/wiki/BeanValidation)。 GWT Bean Validation支持JSR303注释。这意味着您可以在数据传输对象(DTO)上定义约束。我们将它与GWT RequestFactory和GWT Editor Framework一起使用。

现在,如果要更改窗口小部件的样式,可以实现与ValueBoxEditorDecorator类似的功能。下面是一个示例,说明如何为窗口小部件编写这样的装饰器并添加一个css类(如Alex建议的那样):

public class MyWidgetDecorator<T> extends Composite implements
    HasEditorErrors<T>, IsEditor<ValueBoxEditor<T>> {

  // ...

  @UiField
  SimplePanel container;

  @UiField
  DivElement errorLabel;

  // ...

  /**
   * Sets the widget that is to be decorated. 
   */
  @UiChild(limit = 1, tagname = "content")
  public void setContent(ValueBoxBase<T> widget) {
    container.add(widget);

    // ... if using the editor framework, initialize the widget as editor
  }

  public void showErrors(List<EditorError> errors) {
    StringBuilder sb = new StringBuilder();
    for (EditorError error : errors) {
      if (error.getEditor().equals(editor)) {
        sb.append("\n").append(error.getMessage());
      }
    }

    if (sb.length() == 0) {
      errorLabel.setInnerText("");
      errorLabel.getStyle().setDisplay(Display.NONE);

      if (container.getWidget() != null) {
        container.getWidget().removeStyleName("violation"); // remove css class on widget
      }
      return;
    }

    errorLabel.setInnerText(sb.substring(1));
    errorLabel.getStyle().setDisplay(Display.INLINE_BLOCK);

    if (container.getWidget() != null) {
      container.getWidget().addStyleName("violation"); // add css class on widget
    }
  }
}

在方法showErrors(...)中,将名为“验证”的css类添加到窗口小部件(例如文本框)。然后,在css类中,您可以为窗口小部件定义红色边框,以说明输入无效,例如。

顺便说一下,this answer可以找到关于如何使用 GWT编辑器框架的一个很好的解释。

答案 1 :(得分:0)

嗯...不确定你的意思,但为什么不为你的小部件定义一个特殊的错误的新css类,然后在验证失败后将该样式应用于小部件?你确定你需要一个“框架”吗?