Как проверить поле из BeanItem в Vaadin? - PullRequest
0 голосов
/ 07 октября 2010

Я пробую свой первый Hello World с Vaadin прямо сейчас, и я застрял с моей первой простой проверенной формой . Я использую BeanItem в качестве ItemDataSource для своей формы, и я не знаю, как добавить Validator для свойства 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;
    }
}

Ответы [ 2 ]

3 голосов
/ 03 ноября 2010

Вы не можете добавлять валидаторы в собственность напрямую. Валидаторы должны быть добавлены к самому полю (поля формы или автономный пример TexField).

Взгляните на главу 5.17.3 в Книге Ваадина для подтверждения формы: http://vaadin.com/book/-/page/components.form.html (Обратите внимание, что в форме есть также метод getField (id), который вы можете использовать вместо FieldFactory)

0 голосов
/ 08 мая 2012

Я думаю, что вы ищете, это дополнение для проверки Bean.используя его, вы можете сделать что-то вроде добавления @NotNull к вашей собственности, и сгенерированное поле будет автоматически помечено как требуется.

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

...