DynamicForm.setFields doesn't work properly

Hello,

I need to override some fields depending on new/update mode. When I open the form in update mode, I need to use the StaticTextItem instead of some fields in my form.

I wrote the code bellow :

/*
			 * causes the form to fetch its fields from the datasource before
			 * the treatments bellow
			 */
form.getField("firstName");

			if (!isNew && ds instanceof ProfilePersonDS) {
				FormItem[] oldFields = form.getFields();
				FormItem[] newFields = new FormItem[oldFields.length];
				
				
				for (int i = 0; i < oldFields.length; i++) {
					FormItem field = oldFields[i];
					if (field.getName() != null
							&& (field.getName().equals("firstName")
									|| field.getName().equals("lastName")
									|| field.getName().equals("team_id") || field
									.getName().equals("birthday"))) {
						newFields[i] = new StaticTextItem(field.getName());
						newFields[i].setTitle(field.getTitle());
					} else
						newFields[i] = field;
				}
				form.setFields(newFields);
			}

it fetches data perfectly.
but when I modify the data in some fields and call the saveData method :

			saveButton.addClickHandler(new ClickHandler() {

				public void onClick(ClickEvent event) {
					if (form.validate())
						form.saveData(new DSCallbackAdapter(){...});
}});

the form restores the old values and then it calls the saveData method. So it doesn’t take into account data modification.

Thank you for your help !

Don’t try to reuse the fields. Just create a method that returns one set of fields or the other, newly created each time.