Combox Reseting value on Form Commit

Hi All,

I’m having a wired problem. In my test application I have a Person class, which has a name, city and country. So in my form, the value for the City depends on the selected Country.

The classes are:



public class Person {
	private String name;
	private City city;
	private Country country;

        //getters and setters
}

public class Country {

	private String code;
	private String name;
        //getters and setters
}

public class City {
	private String code;
	private String name;

        //getters and setters
}

The problem is, when I try to commit the form, the value of City is set to null on the entity. The code for the Form is:


		final Form form = new Form();
		final Person person = new Person();
		form.setFormFieldFactory(new FormFieldFactory() {

			@Override
			public Field createField(Item item, Object propertyId,
					Component uiContext) {
				if (propertyId.equals("name")) {
					return new TextField((String) propertyId);
				} else {
					return new ComboBox((String) propertyId);
				}
			}
		});
		form.setItemDataSource(new BeanItem(person));

		ComboBox countryComboBox = (ComboBox) form.getField("country");
		countryComboBox.addItem(new Country("US", "United States of America"));
		countryComboBox.addItem(new Country("UK", "United Kindom"));
		countryComboBox.addItem(new Country("MZ", "Mozambique"));

		countryComboBox.addListener(new ValueChangeListener() {
			@Override
			public void valueChange(ValueChangeEvent event) {
				// here we would query the cities for the selected country
				Country selectedCountry = (Country) event.getProperty()
						.getValue();
				List<City> cities = new ArrayList<City>();
				if (selectedCountry.getCode().equals("US")) {
					cities.add(new City("NY", "New York"));
					cities.add(new City("TX", "Texas City"));
				} else if (selectedCountry.getCode().equals("UK")) {
					cities.add(new City("LD", "London"));
				}

				// and then refresh the cities combo
				ComboBox cityComboBox = (ComboBox) form.getField("city");
				cityComboBox.removeAllItems();
				for (City city : cities) {
					cityComboBox.addItem(city);
				}
			}
		});

		Button saveButton = new Button("Save", new ClickListener() {
			@Override
			public void buttonClick(ClickEvent event) {
				form.commit();
				System.out.println(person.getCountry()); // prints the object
				System.out.println(person.getCity()); // prints null???? why?
			}
		});
		form.getFooter().addComponent(saveButton);

When I get the Person object back, after commiting the form, name and country are setted but city is null. The save button handler will print a Country object and null. What I’m doing wrong?

Thank you.

Just for the record. I found out the problem. For some reason Form.commit() is firing ValueChange and so my listener gets invoked clearing the ComboBox. Is this considered a Vaadin problem or I should use a better Aproach? Any Ideas please?

Edrisse

I am having the same problem and I didn’t noticed it until I saw your message. I’ll flag the commiting operation to prevent the combo reload. How did you solve the issue? Do you know alredy if that is a bug or a feture?

Thanks
Aniceto

These extraneous value change events can be considered a bug, and are tracked in
ticket 4394
.

hi… i’m using the version 6.4.7 of the library… and the bug is still there… how i can fix this issue??

At least with my small test programs, I do not see duplicate events in the latest 6.4 version nor in 6.5.

Are you seeing the bug with the code earlier in this thread, or something else. Can you provide a small example on how to reproduce this?