Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
book-of-vaadin.pdf
Скачиваний:
88
Добавлен:
24.03.2015
Размер:
13.43 Mб
Скачать

Binding Components to Data

form.addComponent(new Button("OK", new ClickListener() { @Override

public void buttonClick(ClickEvent event) { try {

binder.commit();

Notification.show("Thanks!"); } catch (CommitException e) {

Notification.show("You fail!");

}

}

}));

// A button to discard the buffer

form.addComponent(new Button("Discard", new ClickListener() { @Override

public void buttonClick(ClickEvent event) { binder.discard(); Notification.show("Discarded!");

}

}));

9.4.5. Binding Fields to a Bean

The BeanFieldGroup makes it easier to bind fields to a bean. It also handles binding to nested beans properties. The build a field bound to a nested bean property, identify the property with dot notation. For example, if a Person bean has a address property with an Address type, which in turn has a street property, you could build a field bound to the property with buildAndBind("Street", "address.street").

The input to fields bound to a bean can be validated using the Java Bean Validation API, as described in Section 9.4.6, “Bean Validation”.The BeanFieldGroup automatically adds a BeanValidator to every field if a bean validation implementation is included in the classpath.

9.4.6. Bean Validation

Vaadin allows using the Java Bean Validation API 1.0 (JSR-303) for validating input from fields bound to bean properties before the values are committed to the bean. The validation is done based on annotations on the bean properties.

Using bean validation requires an implementation of the Bean Validation API, such as Hibernate Validator (hibernate-validator-4.2.0.Final.jar or later) or Apache Bean Validation. The implementation JAR must be included in the project classpath when using the bean validation, or otherwise an internal error is thrown.

Bean validation is especially useful when persisting entity beans with the Vaadin JPAContainer, described in Chapter 21, Vaadin JPAContainer.

Annotations

The validation constraints are defined as annotations. For example, consider the following bean:

// Here is a bean

public class Person implements Serializable { @NotNull

@javax.validation.constraints.Size(min=2, max=10)

String name;

@Min(1)

@Max(130) int age;

252

Binding Fields to a Bean

Binding Components to Data

// ... setters and getters ...

}

For a complete list of allowed constraints for different data types, please see the Bean Validation API documentation [http://docs.oracle.com/javaee/6/tutorial/doc/gircz.html].

Validating the Beans

Validating a bean is done with a BeanValidator, which you initialize with the name of the bean property it should validate and add it the the editor field.

In the following example, we validate a single unbuffered field:

Person bean = new Person("Mung bean", 100);

BeanItem<Person> item = new BeanItem<Person> (bean);

//Create an editor bound to a bean field TextField firstName = new TextField("First Name",

item.getItemProperty("name"));

//Add the bean validator

firstName.addValidator(new BeanValidator(Person.class, "name"));

firstName.setImmediate(true);

layout.addComponent(firstName);

In this case, the validation is done immediately after focus leaves the field. You could do the same for the other field as well.

Bean validators are automatically created when using a BeanFieldGroup.

// Have a bean

Person bean = new Person("Mung bean", 100);

// Form for editing the bean

final BeanFieldGroup<Person> binder =

new BeanFieldGroup<Person>(Person.class); binder.setItemDataSource(bean); layout.addComponent(binder.buildAndBind("Name", "name")); layout.addComponent(binder.buildAndBind("Age", "age"));

// Buffer the form content binder.setBuffered(true);

layout.addComponent(new Button("OK", new ClickListener() { @Override

public void buttonClick(ClickEvent event) { try {

binder.commit();

} catch (CommitException e) {

}

}

}));

Locale Setting for Bean Validation

The validation error messages are defined in the bean validation implementation, in a ValidationMessages.properties file. The message is shown in the language specified with the locale setting for the form. The default language is English, but for example Hibernate Validator contains translations of the messages for a number of languages. If other languages are needed, you need to provide a translation of the properties file.

Bean Validation

253

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]