2012-10-26 7 views
4

GWT 클라이언트 측 유효성 검사를 수행하고 유효성 검사기에서 반환 한 유효성 검사 오류를 표시하는 방법에 대한 문제점이 있습니다. 디버깅했는데 세트에 오류가 있지만 드라이버가 표시하지 않는 것을 볼 수 있습니다. SimpleBeanEditorDriver가 사용됩니다.Editor Framework를 사용하여 GWT 유효성 검사 오류 표시

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)); 
    ... 
} 

GWT 버전에서 테스트되었습니다. 2.4 및 2.5

코드는 https://developers.google.com/web-toolkit/doc/latest/DevGuideValidation에 따라 작성되었지만 편집자는 사용하지 않습니다.

누구나 GWT 유효성 검사 및 편집자가 함께 할 수 있습니까? 누군가 좋은 예를 들어 줄 수 있습니까? 나는 아무 것도 발견 할 수 없었다. 어떤 도움을 환영합니다!

+0

'HasEditorErrors' 편집기를 사용하고 있습니까? –

+0

나는 그렇지 않은 것 같습니다. 나는 편집기 인터페이스 만 구현합니다. 하지만 RequestFactoryEditorDriver를 사용할 때 HasEditorErrors 인터페이스를 구현하지 않고도 유효성 검사 및 오류 표시가 잘 수행되었습니다. – Eugene

+0

'HasEditorErrors' 편집기를 사용하지 않으면 (내장 된 편집기 만'ValueBoxEditorDecorator'입니다), 오류를 처리하여 (아마도) 표시 할 것이 없습니다. 'RequestFactoryEditorDriver'도 예외는 아닙니다. –

답변

1

다음은 편집자/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); 
     } 
    }); 
    } 

샘플.

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

오류 위치를 사용하는 UIBinder 예제입니다.

<t:ValueBoxEditorDecorator errorLocation="RIGHT" ui:field="name"> 
      <t:valuebox> 
       <g:TextBox /> 
      </t:valuebox> 
    </t:ValueBoxEditorDecorator> 

우리가 사용중인 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>&lt;e:valuebox></code> child tag. 
* <p> 
* For example: 
* 
* <pre> 
* &#064;UiField 
* ValueBoxEditorDecorator&lt;String&gt; name; 
* </pre> 
* 
* <pre> 
* &lt;e:ValueBoxEditorDecorator ui:field='name'> 
* &lt;e:valuebox> 
*  &lt;g:TextBox /> 
* &lt;/e:valuebox> 
* &lt;/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); 
    } 
}