Here is a simple example of how we use the / HasEditorError and ConstraintViolations editors. I also included a sample of our ValueBoxEditorDecorator, which allows us to display an error message.
Our activity
@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); } }); }
An example from our presentation.
@UiField ValueBoxEditorDecorator<String> name;
UIBinder example using error location.
<t:ValueBoxEditorDecorator errorLocation="RIGHT" ui:field="name"> <t:valuebox> <g:TextBox /> </t:valuebox> </t:ValueBoxEditorDecorator>
Used by us 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; public class ValueListBoxEditorDecorator<T> extends Composite implements HasEditorErrors<T>, IsEditor<TakesValueEditor<T>> { public static enum ErrorPanelLocation { LEFT, RIGHT; } SimplePanel contents = new SimplePanel(); @Ignore Label errorLabel = new Label(); HorizontalPanel layout = new HorizontalPanel(); private TakesValueEditor<T> editor; @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); } } public ValueListBoxEditorDecorator(ValueListBox<T> widget, TakesValueEditor<T> editor) { this(ErrorPanelLocation.RIGHT); contents.add(widget); this.editor = editor; } public TakesValueEditor<T> asEditor() { return editor; } public void setEditor(ValueBoxEditor<T> editor) { this.editor = editor; } @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); } 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); } }
source share