2010-11-04 45 views
19

我试图与GWT 2.1.0的新GWT编辑器框架集成。我也想将我的验证检查添加到框架中。然而,我正在努力寻找一个体面的例子如何做到这一点。如何使用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)); 
    } 

} 

在哪一点我有打电话给我验证,我如何与它进行集成?

更新:

我实际上是寻找一种方法来检索与关键属性路径地图,和值编辑器。代理上有一个路径字段,但这不是编辑对象中的路径,而是编辑器类中的路径。

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

回答

8

标注你豆contstrants(见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); 

按照此模式为要在客户端上验证的对象创建验证程序。 (见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文件告诉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

+2

太糟糕了,这还没有绑定到编辑框架,以提供验证消息旁边的输入字段。 – Jan 2011-03-30 12:48:39

+1

从gwt-2.4.0-rc1开始已经尝试:请参阅com.google.gwt.editor.client.impl.BaseEditorDriver.setConstraintViolations(...) 尽管由于奇怪的泛型而无法在客户端使用在方法参数中。 – 2011-08-20 10:10:16

0

验证在GWT中不存在,它将在下一个版本AFAIK中发布。目前支持GWT验证的服务器端JSR-303和客户端JSR-303支持即将推出。因此,您必须手动进行验证。如果你遵循MVP模型,我认为这个验证逻辑会存在于你的Presenter中。

+0

我想要做的是将我的验证系统插入到GWT编辑器框架中。你在编辑器上有像recordError和showErrors这样的操作。我正在寻找正确的位置来触发验证,并将验证错误连接到我用来配置编辑器的路径。 – Jan 2010-11-06 09:08:44

1

我有exaclty同样的问题。

该文件不清楚。

我目前正在做的是通过用我想复制的小部件扩展它们来重新创建一些小部件。之后我实现了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; 
} 
} 

最好的办法是简单地定制验证添加到现有的小部件,而不是覆盖他们更大,但我不知道知道该怎么做!

+0

我想在外部进行验证,并在稍后添加我的验证错误。目前,我在每个编辑器中创建了一些自定义代码,以将propertyPath绑定到一个小部件。但是,这些信息已经存在于系统的某个地方,我似乎无法将它从系统中清除出去。 – Jan 2010-11-13 16:05:24

0

这很麻烦,但要获得编辑器的路径,您可以实现HasEditorDelegate(它将使您可以访问委托),然后将委托转换为具有公共String getPath()方法的AbstractEditorDelegate。

虽然我不认为有可能做外部验证;验证发生在一个值从外地读点编辑器(见ValueBoxEditor - 这个编辑器使用getDelegate()recordError引发错误)。一种选择我认为是使用AbstractEditorDelegate访问调用flushErrors(列表)并创建EditorErrors的该列表自己。要做到这一点,你需要知道你的每个场路径;对它们进行硬编码是不可取的,但我没有看到通过编辑的属性或类似的东西来查看字段的方法。

可能承担寻找到另一种方法是使用requestfactory本次提交的往返验证:

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

2

我已经通过添加附加DriverWrapper类,它做了一些类似的现有的驱动程序和验证程序,并添加一个flush方法,首先将其委托给底层的Driver flush,然后调用Validator。然后,任何返回的错误都将被添加到编辑器,使用新的访问者,类似于现有的冲洗器的工作方式。这意味着在字段旁边显示错误的现有装饰器继续工作。

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

做了类似的提供动态验证。由于编辑器访问者模式,工作得很好。 – logan 2012-01-29 06:55:50

+0

@logan,你能告诉我你是如何实现动态验证的吗?如果是嵌套编辑器,我很难以简明的方式做到这一点。 – expert 2012-03-21 03:00:19

+0

@ruslan - 我一直在想让它成为开源,但我还没有找到时间。你还有兴趣吗? – logan 2012-04-13 16:50:16