首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用GWT编辑器框架进行验证?

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

Stack Overflow用户
提问于 2010-11-04 23:59:23
回答 5查看 14.9K关注 0票数 19

我正在尝试与GWT 2.1.0的新GWT编辑器框架集成。我还想将我的验证检查添加到框架中。然而,我正在努力寻找一个好的例子来做这件事。

目前,我有以下代码:

<!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> 

对于我的编辑:

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

}

在什么情况下,我必须调用我的验证器,以及如何与它集成?

更新:

我实际上是在寻找一种方法来检索一个以属性路径为键,以编辑器为值的映射。代理上有一个路径字段,但这不是已编辑对象中的路径,而是编辑器类中的路径。

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

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-02-11 15:46:37

使用contstrants注释您的bean(参见Person.java)

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

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

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

遵循此模式为要在客户机上验证的对象创建Validator。(参见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);
  }
}

包括您的验证提供程序的模块。在gwt modle文件中添加replace-with标记,告诉GWT使用刚刚定义的验证器(参见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

票数 8
EN

Stack Overflow用户

发布于 2011-08-12 13:31:11

我已经做了类似的事情,添加了一个附加的DriverWrapper类,该类接受现有的驱动程序和验证器,并添加了一个flush方法,该方法首先委托底层驱动程序刷新,然后调用验证器。然后使用新的访问器将返回的任何错误添加到编辑器中,类似于现有Flusher的工作方式。这意味着在字段旁边显示错误的现有装饰器可以继续工作。

/**
 * Wraps a Driver and provides validation using gwt-validation (JSR 303).
 * When calling flush, this will use the provided IValidator to validate the data
 * and use the InvalidConstraintValidationVisitor to add the errors to the delegates.
 * @see InvalidConstraintValidationVisitor
 * @param <T> the data type for the editor
 * @param <D> the driver type
 */
public class ValidationDriverWrapper<T extends IsValidatable<T>, D extends EditorDriver<T>> {
private IValidator<T> validator;
private D driver;

/**
 * Constructor, both parameters are required.
 * @param driver The driver to use to flush the underlying data.
 * @param validator The validator to use to validate the data.
 */
public ValidationDriverWrapper(D driver, IValidator<T> validator) {
    this.validator = validator;
    this.driver = driver;
}

/**
 * Flushes the underlying Driver and then calls the validation on the underlying Validator, cascading errors as EditorErrors
 * onto the delegates, using InvalidContraintValidationVisitor.
 */
public void flush()
{
    T data = driver.flush();
    Set<InvalidConstraint<T>> errors = validator.validate(data);
    Set<InvalidConstraint<T>> extraErrors = data.validate();
    if(extraErrors != null && !extraErrors.isEmpty())
    {
        errors.addAll(extraErrors);
    }
    driver.accept(new InvalidConstraintValidationVisitor<T>(errors));
}
票数 2
EN

Stack Overflow用户

发布于 2010-11-12 22:22:53

我也有同样的问题。

文档对此并不清楚。

我现在正在做的是通过用我想要复制的小部件扩展它们来重新创建一些小部件。之后,我实现了LeafValueEditor和HasEditorDelegate来覆盖getValue()。

在getValue()中,使用验证器并根据需要调用yourDelegate.recordError()。

如下所示:一个小整数框,用于检查值是否不大于10。

public class IntegerField extends ValueBox<Integer> implements LeafValueEditor<Integer>, HasEditorDelegate<Integer>
{
private EditorDelegate<Integer> delegate;

public IntegerField()
{
    super(Document.get().createTextInputElement(), IntegerRenderer.instance(), IntegerParser.instance());

    setStyleName("gwt-TextBox");

}

@Override
public Integer getValue()
{
    Integer value = super.getValue();

    if (value > 10)
    {
        delegate.recordError("too big integer !", value, null);
    }

    return value;
}

@Override
public void setValue(Integer value)
{
    super.setValue(value);
}

@Override
public void setDelegate(EditorDelegate<Integer> delegate)
{
    this.delegate = delegate;
}
}

最好的方法是简单地向现有的小部件添加自定义验证,而不是覆盖它们,但我不知道怎么做!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4098579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档