使用编辑器框架显示GWT验证错误
我做GWT客户端验证,我有一个问题,就是如何显示验证程序返回的验证错误。我调试了它,我可以看到该集包含错误,但驱动程序没有显示它们。使用SimpleBaneditorDriver使用编辑器框架显示GWT验证错误,gwt,gwt-editors,gwt-validation,Gwt,Gwt Editors,Gwt Validation,我做GWT客户端验证,我有一个问题,就是如何显示验证程序返回的验证错误。我调试了它,我可以看到该集包含错误,但驱动程序没有显示它们。使用SimpleBaneditorDriver Entry Entry = driver.flush(); Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); Set<ConstraintViolation<Entry>> violat
Entry Entry = driver.flush();
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<Entry>> violations = validator.validate(Entry, Default.class);
if (violations.size() > 0) {
driver.setConstraintViolations(new ArrayList<ConstraintViolation<?>>(violations));
...
}
Entry=driver.flush();
Validator Validator=Validation.buildDefaultValidatorFactory().getValidator();
设置冲突=validator.validate(条目,Default.class);
如果(冲突.size()>0){
driver.setConstraintViolations(new ArrayList这里是一个简单的示例,说明了我们如何使用编辑器/HasEditorError和ConstraintViolations。我还包括了ValueBoxEditorDecorator中的一个示例,它允许我们布局错误消息
我们的活动
@Override
public void onSave() {
RequestFactoryEditorDriver<DashboardModelProxy, ?> driver = display.getDriver();
RequestContext context = driver.flush();
context.fire(new Receiver<Void>() {
@Override
public void onSuccess(Void response) {
Place previousPlace = clientFactory.getPlaceController().getPreviousPlace();
clientFactory.getPlaceController().goTo(previousPlace);
}
@Override
public void onFailure(ServerFailure error) {
display.showError(error.getMessage());
}
@Override
public void onConstraintViolation(Set<ConstraintViolation<?>> violations) {
display.getDriver().setConstraintViolations(violations);
}
});
}
*
*
*e:ValueBoxeditorDecoratorUI:field='name'>
*e:valuebox>
*g:TextBox/>
*/e:valuebox>
*/e:ValueBoxEditorDecorator>
*
*
*@param
*正在编辑的数据类型
*/
公共类ValueListBoxEditorDecorator扩展了复合实现HasEditorErrors,IsEdit{
/**
*文本相对于分页按钮的位置。
*/
公共静态枚举ErrorPanelLocation{
左,右,;
}
SimplePanel内容=新SimplePanel();
@忽略
Label errorLabel=新标签();
HorizontalPanel布局=新建HorizontalPanel();
私人图书馆价值编辑;
/**
*构造ValueBoxEditorDecorator。
*/
@UIC构造函数
公共价值清单BoxeditorDecorator(ErrorPanelLocation errorLocation){
initWidget(布局);
setStyleName(“gwt ValueBoxEditorDecorator”);
errorLabel.setStyleName(“gwt ValueBoxEditorDecorator错误”);
errorLabel.getElement().getStyle().setDisplay(Display.NONE);
if(errorLocation==ErrorPanelLocation.RIGHT){
布局。添加(内容);
布局。添加(错误标签);
}否则{
布局。添加(错误标签);
布局。添加(内容);
}
}
/**
*使用{@link ValueBoxBase}小部件构造ValueBoxEditorDecorator
*和{@link ValueBoxEditor}编辑器。
*
*@param小部件
*小部件
*@param编辑器
*编辑
*/
公共价值列表框编辑器(ValueListBox小部件,TakesValueEditor编辑器){
这个(ErrorPanelLocation.RIGHT);
contents.add(小部件);
this.editor=editor;
}
/**
*返回关联的{@link ValueBoxEditor}。
*
*@return一个{@link ValueBoxEditor}实例
*@see#setEditor(ValueBoxEditor)
*/
public TakesValue Editor AseEditor(){
返回编辑器;
}
/**
*设置关联的{@link ValueBoxEditor}。
*
*@param编辑器
*{@link ValueBoxEditor}实例
*@see#asEditor()
*/
public void集合编辑器(ValueBoxEditor编辑器){
this.editor=editor;
}
/**
*设置EditorPanel将显示的小部件。此方法将
*自动调用{@link#setEditor}。
*
*@param小部件
*{@link ValueBoxBase}小部件
*/
@UiChild(limit=1,tagname=“valuebox”)
public void setValueBox(ValueBoxBase小部件){
contents.add(小部件);
setEditor(widget.asEditor());
}
公共无效clearErrors(){
errorLabel.setText(“”);
errorLabel.getElement().getStyle().setDisplay(Display.NONE);
}
/**
*默认实现将显示但不使用接收到的错误
*其{@link EditorError#getEditor()getEditor()}方法返回
*编辑器已传递到{@link#setEditor}。
*
*@param错误
*{@link EditorError}实例的列表
*/
公共无效错误(列表错误){
StringBuilder sb=新的StringBuilder();
对于(编辑错误:错误){
if(error.getEditor().equals(编辑器)){
sb.append(“\n”).append(error.getMessage());
}
}
如果(sb.length()==0){
clearErrors();
返回;
}
errorLabel.setText(sb子字符串(1));
errorLabel.getElement().getStyle().setDisplay(Display.INLINE_块);
}
}
此wiki可能会帮助您:
尽管它集成了编辑器、验证器和RequestFactory。
我创建了一个使用它的Maven原型:
您是否使用了任何
HasEditorErrors
编辑器?看起来我没有。我只实现了编辑器界面。但是,当我使用RequestFactoryEditorDriver时,在没有实现HasEditorErrors界面的情况下,错误的验证和显示工作得很好。如果您不使用任何HasEditorErrors
编辑器(只有一个内置的是ValueBoxEditorDecorator
),那么就没有任何东西可以处理错误来(可能)显示它们。RequestFactoryEditorDriver
也不例外。我查看了界面,不确定应该在淋浴错误(列出editorErrors)中放入什么代码方法?看起来每个字段都应该在decorator中,我应该为编辑器中的每个控件调用fieldDecorator.batherrors(editorErrors)?对吗?
/**
* Name component for the name of the analytics operation.
* This also implements {@link HasEditorErrors so it can show
* constraint violations when an error occurs.
*/
@UiField
ValueBoxEditorDecorator<String> name;
<t:ValueBoxEditorDecorator errorLocation="RIGHT" ui:field="name">
<t:valuebox>
<g:TextBox />
</t:valuebox>
</t:ValueBoxEditorDecorator>
import java.util.List;
import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.editor.client.EditorError;
import com.google.gwt.editor.client.HasEditorErrors;
import com.google.gwt.editor.client.IsEditor;
import com.google.gwt.editor.client.adapters.TakesValueEditor;
import com.google.gwt.editor.ui.client.adapters.ValueBoxEditor;
import com.google.gwt.uibinder.client.UiChild;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.ValueBoxBase;
import com.google.gwt.user.client.ui.ValueListBox;
/**
* This is a copy of the original ValueBoxEditorDecorator in the gwt source The
* reason we are not using it is because it did not support laying out the error
* panel in a different location.
*
*
* A simple decorator to display leaf widgets with an error message.
* <p>
* <h3>Use in UiBinder Templates</h3>
* <p>
* The decorator may have exactly one ValueBoxBase added though an
* <code><e:valuebox></code> child tag.
* <p>
* For example:
*
* <pre>
* @UiField
* ValueBoxEditorDecorator<String> name;
* </pre>
*
* <pre>
* <e:ValueBoxEditorDecorator ui:field='name'>
* <e:valuebox>
* <g:TextBox />
* </e:valuebox>
* </e:ValueBoxEditorDecorator>
* </pre>
*
* @param <T>
* the type of data being edited
*/
public class ValueListBoxEditorDecorator<T> extends Composite implements HasEditorErrors<T>, IsEditor<TakesValueEditor<T>> {
/**
* The location of the text relative to the paging buttons.
*/
public static enum ErrorPanelLocation {
LEFT, RIGHT;
}
SimplePanel contents = new SimplePanel();
@Ignore
Label errorLabel = new Label();
HorizontalPanel layout = new HorizontalPanel();
private TakesValueEditor<T> editor;
/**
* Constructs a ValueBoxEditorDecorator.
*/
@UiConstructor
public ValueListBoxEditorDecorator(ErrorPanelLocation errorLocation) {
initWidget(layout);
setStyleName("gwt-ValueBoxEditorDecorator");
errorLabel.setStyleName("gwt-ValueBoxEditorDecorator-error");
errorLabel.getElement().getStyle().setDisplay(Display.NONE);
if (errorLocation == ErrorPanelLocation.RIGHT) {
layout.add(contents);
layout.add(errorLabel);
} else {
layout.add(errorLabel);
layout.add(contents);
}
}
/**
* Constructs a ValueBoxEditorDecorator using a {@link ValueBoxBase} widget
* and a {@link ValueBoxEditor} editor.
*
* @param widget
* the widget
* @param editor
* the editor
*/
public ValueListBoxEditorDecorator(ValueListBox<T> widget, TakesValueEditor<T> editor) {
this(ErrorPanelLocation.RIGHT);
contents.add(widget);
this.editor = editor;
}
/**
* Returns the associated {@link ValueBoxEditor}.
*
* @return a {@link ValueBoxEditor} instance
* @see #setEditor(ValueBoxEditor)
*/
public TakesValueEditor<T> asEditor() {
return editor;
}
/**
* Sets the associated {@link ValueBoxEditor}.
*
* @param editor
* a {@link ValueBoxEditor} instance
* @see #asEditor()
*/
public void setEditor(ValueBoxEditor<T> editor) {
this.editor = editor;
}
/**
* Set the widget that the EditorPanel will display. This method will
* automatically call {@link #setEditor}.
*
* @param widget
* a {@link ValueBoxBase} widget
*/
@UiChild(limit = 1, tagname = "valuebox")
public void setValueBox(ValueBoxBase<T> widget) {
contents.add(widget);
setEditor(widget.asEditor());
}
public void clearErrors() {
errorLabel.setText("");
errorLabel.getElement().getStyle().setDisplay(Display.NONE);
}
/**
* The default implementation will display, but not consume, received errors
* whose {@link EditorError#getEditor() getEditor()} method returns the
* Editor passed into {@link #setEditor}.
*
* @param errors
* a List of {@link EditorError} instances
*/
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) {
clearErrors();
return;
}
errorLabel.setText(sb.substring(1));
errorLabel.getElement().getStyle().setDisplay(Display.INLINE_BLOCK);
}
}