I am observing that with Vaadin 7.0.2 the only BeanFieldGroup validation that worked in my application was with a field set with @Digits annotation.
So I took the Vaadin BeanValidationExample from the book and put it in my UI as a view to test. With the fieldgroup() example, validation doesn’t happen for @Size, @Min, @Max or @NotNull. Even with BeanValidationExample, the only validation that happens is when text is entered for an int field.
Reading through forums, someone suggested turning the buffering off. In the BeanValidationExample if I do form.setBuffered(false), I get
com.vaadin.server.DefaultErrorHandler doDefault
SEVERE:
com.vaadin.data.Buffered$SourceException
at com.vaadin.ui.AbstractField.setValue(AbstractField.java:509)
at com.vaadin.ui.AbstractTextField.changeVariables(AbstractTextField.java:181)
Are these known issues that have tickets or should I open one?
Here’s the minor change adaptation of BeanValidationExample class to add it as a view for testing
Thanks
Nilesh
package com.capario.tools.eragapadmin.ui;
import java.io.Serializable;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import com.vaadin.data.fieldgroup.BeanFieldGroup;
import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.validator.BeanValidator;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Notification;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
public class BeanValidationExample extends CustomComponent implements View {
/**
*
*/
private static final long serialVersionUID = 1L;
private VerticalLayout layout;
public BeanValidationExample() {
layout = new VerticalLayout();
setCompositionRoot(layout);
}
// BEGIN-EXAMPLE: datamodel.itembinding.beanvalidation.basic
// Here is a bean
public class Person implements Serializable {
private static final long serialVersionUID = -1520923107014804137L;
@NotNull
@javax.validation.constraints.Size(min = 2, max = 10)
String name;
@Min(1)
@Max(130)
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
void basic() {
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);
// END-EXAMPLE: datamodel.itembinding.beanvalidation.basic
}
void fieldgroup() {
// BEGIN-EXAMPLE: datamodel.itembinding.beanvalidation.fieldgroup
// Have a bean
Person bean = new Person("Mung bean", 100);
// Form for editing the bean
final BeanFieldGroup<Person> form = new BeanFieldGroup<Person>(Person.class);
form.setItemDataSource(bean);
layout.addComponent(form.buildAndBind("Name", "name"));
layout.addComponent(form.buildAndBind("Age", "age"));
// Buffer the form content
form.setBuffered(false);
layout.addComponent(new Button("OK", new ClickListener() {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void buttonClick(ClickEvent event) {
try {
form.commit();
} catch (CommitException e) {
Notification.show("Validation Error", e.getCause() != null ? e.getCause().getMessage() : e.getMessage(),
Notification.Type.ERROR_MESSAGE);
}
}
}));
// END-EXAMPLE: datamodel.itembinding.beanvalidation.fieldgroup
}
@Override
public void enter(ViewChangeEvent event) {
// basic();
fieldgroup();
}
}