我正在尝试与 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));
}
}
我必须在什么时候调用我的 validator 以及如何与它集成?
更新:
我实际上正在寻找一种方法来检索以属性路径为键并以编辑器为值的 map 。委托(delegate)上有一个路径字段,但这不是编辑对象内的路径,而是编辑器类中的路径。
有人知道是否可以做这样的事情吗?
最佳答案
用 contstrants 注释你的 bean(见 Person.java)
public class Person {
@Size(min = 4)
private String name;
}
使用标准验证 Bootstrap 在客户端获取 validator 并验证您的对象(参见 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 模型文件中添加替换标记,告诉 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>
关于java - 如何使用 GWT 编辑器框架进行验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4098579/