0

我现在正在使用Vaadin尝试我的第一个Hello World ,但我坚持使用我的第一个简单验证的 Form。我使用 a作为我的表单,但我不知道如何为 bean 属性添加验证器。BeanItemItemDataSource

我的问题

如何获得Fieldbean 中属性的实际值?我需要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"));
    }
}

你好世界豆

package vaadinapp.hello;

public class HelloWorldBean {
    String greeting;

    public String getGreeting() {
        return greeting;
    }

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

2 回答 2

3

您不能直接将验证器添加到属性。必须将验证器添加到字段本身(例如,表单中的字段或独立的 TexField)。

查看 Vaadin 中的第 5.17.3 章以验证表单: http: //vaadin.com/book/-/page/components.form.html (请注意,其中还有 getField(id) 方法您可以使用的表单而不是 FieldFactory)

于 2010-11-02T21:43:55.287 回答
0

我认为您正在寻找的是 Bean 验证插件。使用它,您可以执行一些操作,例如将 @NotNull 添加到您的属性中,生成的字段将自动标记为必填项。

https://vaadin.com/directory#addon/vaadin-bean-validation

于 2012-05-07T23:21:18.713 回答