2010-10-07 70 views
0

我正在嘗試我的第一個Hello WorldVaadin現在,我堅持我的第一個簡單的驗證表格。我使用BeanItem作爲我的表單的ItemDataSource,我不知道如何爲bean屬性添加Validator。如何從Vaadin中的BeanItem驗證字段?

我的問題

我怎樣才能得到我的bean屬性的實際Field?我需要在場上撥打addValidator(),但我只能在Form上獲得。

HelloWorldForm

package vaadinapp.hello; 

import com.vaadin.data.util.BeanItem; 
import com.vaadin.ui.Alignment; 
import com.vaadin.ui.Button; 
import com.vaadin.ui.Form; 
import com.vaadin.ui.HorizontalLayout; 
import com.vaadin.ui.Label; 
import com.vaadin.ui.VerticalLayout; 

public class HelloWorldForm extends Form { 
    HelloWorldBean data = new HelloWorldBean(); 

    public HelloWorldForm() { 
     setCaption("Hello World"); 
     setDescription("This is a simple form that lets you enter your name and displays a greeting."); 

     setItemDataSource(new BeanItem(data)); 

     setFooter(new VerticalLayout()); 
     getFooter().addComponent(
       new Label("This is the footer area of the Form. You can use any layout here. This is nice for buttons.")); 
     // Have a button bar in the footer. 
     HorizontalLayout okbar = new HorizontalLayout(); 
     okbar.setHeight("25px"); 
     getFooter().addComponent(okbar); 
     // Add an Ok (commit), Reset (discard), and Cancel buttons 
     // for the form. 
     Button okbutton = new Button("OK", this, "commit"); 
     okbar.addComponent(okbutton); 
     okbar.setComponentAlignment(okbutton, Alignment.TOP_RIGHT); 
     okbar.addComponent(new Button("Reset", this, "discard")); 
     okbar.addComponent(new Button("Cancel")); 
    } 
} 

HelloWorldBean

package vaadinapp.hello; 

public class HelloWorldBean { 
    String greeting; 

    public String getGreeting() { 
     return greeting; 
    } 

    public void setGreeting(String greeting) { 
     this.greeting = greeting; 
    } 
} 

回答

3

您無法直接驗證添加到一個屬性。必須將驗證器添加到字段本身(表單或獨立TexField示例中的字段)。

看看在Vaadin之書的章節5.17.3用於驗證表單:http://vaadin.com/book/-/page/components.form.html (請注意,也有getfield命令(ID)方法的形式,你可以使用,而不是FieldFactory)