2011-05-12 69 views
5

有沒有人知道編程器和jsr 303驗證如何與GWT 2.3 一起工作?驗證API已添加到gwt sdk。但我無法使用編輯器框架驗證實體。無論我做什麼 我都不會從客戶端或服務器端拋出錯誤。帶編輯器框架的GWT驗證器

這裏是一個代碼段:

public class P { 

    public P() {} 

    @Size(min=4) 
    private String name; 

    public void setName(String name) { 
    this.name = name; 
    } 

    public String getName() { 
    return name; 
    } 

} 

PEditor

public class PEditor extends Composite implements Editor<P> { 

    private static PEditorUiBinder uiBinder = GWT.create(PEditorUiBinder.class); 

    interface PEditorUiBinder extends UiBinder<Widget, PEditor> {} 

    @UiField 
    TextBox name; 

    public PEditor() { 
    initWidget(uiBinder.createAndBindUi(this)); 
    } 

} 

PEditor pEditor; 
    interface Driver extends SimpleBeanEditorDriver<P, PEditor> {} 

    Driver driver = GWT.<Driver> create(Driver.class); 

    public void onModuleLoad() { 

    pEditor = new PEditor(); 
    driver.initialize(pEditor); 
    P p = new P(); 
    driver.edit(p); 
    pEditor.name.setText("G"); 
    driver.flush(); 

    if(driver.hasErrors()) { 
     List<EditorError> errors = driver.getErrors(); 

     for (EditorError error : errors) { 
      System.out.println(error.getMessage()); 

     } 

    } 
    } 

感謝您的幫助

回答

2

驗證API(至少2.3版本)不會爲您構建客戶端代碼 - 它是一種可集成在服務器上的工具,以便在某些情況下使服務器反吐錯誤。

EditorDriver.hasErrors()的調用只是爲了檢查是否有代碼告訴本地代表是否有錯誤 - 客戶端驗證可以通過這個來實現。

現在最自動的情況是使用RequestFactory時 - 如果您的服務器classpath上有javax.validation jar(api和sources)以及驗證庫(hibernate-validator和apache的bval是兩個這樣的庫),Receiver回調將調用onViolation

隨着RequestFactory被用來從服務器獲取違規,該RequestFactoryEditorDriver然後可以用來推錯誤的UI,但使用HasEditorErrors編輯器實例和包裝,像ValueBoxEditorDecorator的,或只是通過某種機制通知用戶(警告,橫幅,你的debug sys.out.println等),當調用onViolation時。

如果使用RPC,您可以自行運行服務器驗證,並且(從2.3開始)通過驗證過程調用driver.setConstraintViolations和服務器上生成的ConstraintViolation對象。