简体   繁体   English

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

[英]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. 我正在尝试与GWT 2.1.0的新GWT Editor框架集成。 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? 有人知道是否有可能做这样的事情?

Annotate you beans with contstrants (see Person.java ) 使用contstrants注释bean(请参阅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并验证您的对象(请参阅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 ) (参见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 ) 在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>

Source 资源

I have done something similar to this by adding an additional DriverWrapper class that takes the existing Driver and Validator and adds a flush method that first delegates to the underlying Driver flush and then calls the Validator. 我通过添加一个额外的DriverWrapper类来完成类似的操作,该类使用现有的Driver和Validator并添加一个flush方法,该方法首先委托给底层的Driver flush,然后调用Validator。 Any errors returned are then added to the Editors using a new visitor, similar to the way the existing Flusher works. 然后,使用新访问者将返回的任何错误添加到编辑器,类似于现有Flusher的工作方式。 This means the existing decorators that display the errors next to the fields continue to work. 这意味着显示字段旁边的错误的现有装饰器将继续工作。

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

I've got exaclty the same problem. 我有同样的问题。

The documentation is not clear about it. 文档不清楚。

What I'm currently doing is to recreate some widgets by extending them with widget I want to copy. 我目前正在做的是通过使用我想要复制的小部件来扩展它们来重新创建一些小部件。 After it I implement LeafValueEditor and HasEditorDelegate to override getValue(). 之后,我实现了LeafValueEditor和HasEditorDelegate来覆盖getValue()。

In getValue(), use your validator and call if needed yourDelegate.recordError(). 在getValue()中,使用验证器并在需要时调用yourDelegate.recordError()。

Something like this: a little integer box which check that value is not greater than 10. 像这样:一个小整数框,检查该值不大于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;
}
}

The best approach is to simply add custom verification to existing widgets and not override them but I don't know how to do it ! 最好的方法是简单地将自定义验证添加到现有小部件而不是覆盖它们,但我不知道该怎么做!

Validation doesn't exist in GWT yet, it's coming in the next release AFAIK. GWT中尚不存在验证,它将在下一个版本的AFAIK中出现。 The current support for validation in GWT is server side JSR-303 and client side JSR-303 support is coming soon. GWT当前对验证的支持是服务器端JSR-303,客户端JSR-303支持即将推出。 Therefore, you'll have to do the validation manually. 因此,您必须手动进行验证。 If you follow the MVP model, I think this validation logic would live in your Presenter. 如果您遵循MVP模型,我认为此验证逻辑将存在于您的Presenter中。

It's messy but to get the path of an Editor you could implement HasEditorDelegate (which will give you access to the delegate) and then cast the Delegate to AbstractEditorDelegate, which has a public String getPath() method. 这很乱,但要获得编辑器的路径,您可以实现HasEditorDelegate(它将允许您访问委托),然后将Delegate转换为AbstractEditorDelegate,它具有公共String getPath()方法。

I don't think it is possible to do external validation though; 我认为不可能进行外部验证; validation happens in the editor at the point that a value is read from the field (see ValueBoxEditor - this editor uses getDelegate().recordError to raise an error). 验证在编辑器中发生,即从字段读取值(请参阅ValueBoxEditor - 此编辑器使用getDelegate()。recordError来引发错误)。 One option I did consider was to use the AbstractEditorDelegate access to call flushErrors(List) and to create that list of EditorErrors myself. 我考虑过的一个选项是使用AbstractEditorDelegate访问来调用flushErrors(List)并自己创建EditorErrors列表。 To do that you need to know each of your field paths; 要做到这一点,你需要了解每个场路径; hardcoding them is hardly desirable, but I don't see a way of looking up the Field by the edited property or anything like that. 对它们进行硬编码是不可取的,但是我没有看到通过编辑的属性或类似的东西查找字段的方法。

An alternative approach that might bear looking into is this submission for round trip validation using the requestfactory: 可能需要考虑的另一种方法是使用requestfactory进行往返验证的提交:

http://groups.google.com/group/google-web-toolkit-contributors/browse_thread/thread/5be0bda80547ca5a http://groups.google.com/group/google-web-toolkit-contributors/browse_thread/thread/5be0bda80547ca5a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM