Vaadin 7.0.2 BeanFieldGroup validation not working

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();
	}
}


RESOLVED

After realizing that I need to manually supply bean validation libraries, I went through some trial and error to make it work.

Basically I added following JARs to my WEB-INF\lib

  • validation-api-1.0.0.GA.jar
  • jboss-logging-3.1.2.GA.jar
  • slf4j-api-1.5.6.jar
  • slf4j-simple-1.5.6.jar
  • hibernate-validator-4.3.1.Final.jar

Now there is just one known issue - multiple validators are attached, every time setItemDataSource is called - which is a known issue and there are a few tickets for it

Yeah, you need a validation library.

Oh, the BeanFieldGroup demo isn’t again working properly at the demo site, even though it works on my workstation. I’m probably too having some problem with the validation library. Oh well, I’ll take a look at it later.

thanks, this thread saved my day. I used Ivy dependency for this


<dependency org="javax.validation" name="validation-api" rev="1.0.0.GA" />
<dependency org="org.jboss.logging" name="jboss-logging" rev="3.1.2.GA"/>
<dependency org="org.slf4j" name="slf4j-log4j12" rev="1.5.6"/>
<dependency org="org.slf4j" name="slf4j-simple" rev="1.5.6"/>
<dependency org="org.hibernate" name="hibernate-validator" rev="4.3.1.Final"/>