Working with Forms

Hello

i have been reading about Forms:[list]
Form as a User Interface Component
[/list][list]
Binding Form to Data
[/list][list]
Validating Form Input
[/list]

it was very nice and simplifies a lot of work with some few lines of code…

however i tried the way described in Reference Manual and it does not work…

thanks in advance

What part did not work? I’d be happy to help if you give more details.

The usual way to do it (or how I usually do it is)

  1. set up form

Form form = new Form();
  1. create a field factory

class MyFieldFactory implements FieldFactory{
...
public Field createField(Item item, Object propertyId, Component uiContext){
  String pid = (String) propertyId
  if(pid.equals("name")){
    return new TextField("Name of Person");
  } else if (pid.equals("role") {
    return new Select("Role in team",listOfRoleOptions);
  } etc. for all fields
  1. give the field factory to the form

form.setFieldFactory(new MyFieldFactory())
  1. set the attributes for the form

form.setWriteThrough(true);
form.setImmediate(true);
form.setLayout(new GridLayout(2,3));
  1. give the form a object to work on.

BeanItem item = new BeanItem(myObject);
form.setItemDataSource(item);

Hope you’ll get to a start with this.

mwandu, Im using forms/data binding/validation with no issues, so if you’ll describe your problems in more detail, Im sure we’ll be able to help.

Another issue concerning forms…

Let’s say I have a DTO something like this:


public class MyDTO {
  public String name;
  public List<String> emails; // accessor methods removed for simplicity
}

How should I handle this kind of object in a form so that I could have a TextField for each element in the list? createField methods in FieldFactory always return a field… I tried to wrap the list items in another Form (as it is a Field object), but at least the layout screwed up, and I’m not quite sure the data access went right either :slight_smile:

I’d like to have the form commit affecting the whole Form, whether it has a subform or not.

And instead of the List there might also be a Map, but that shouldn’t be a problem if the List version works (the keys in the map would be constans values).

Ville,

I suppose, you have to create your own custom field, which will take and give a List<String> object as its value. This field can act like a calendar field, so when you click a button or entire field, a popup window opens where you can add/remove emails. Then, use a custom FieldFactory to attach your custom field into a form.

And that would mean that I’d have to create the whole widgetset stuff also? Oh no, there has to be an easier solution…

Your popup idea got me thinking, that if there’s no easier way around, then maybe I should use popups to show these lists. And in the popup there would be a form. Of course this would mean that the popup and the main form wouldn’t have a common commit method, but I think that could be handled manually.

No, to create a custom field you do not need to do a GWT programming and compile a widgetset.

I would just put a table with all the list entries, a textfield and add/remove -buttons next to the field. Everything doesn’t have to be in the same form. You just have to pass along the table’s collection at the same time you do form.commit();

Sorry to post a reply on such an old topic, but I’m working on 6.3 nightly now and have completed the tutorials.

I now have my own basic system working, listing my “Group” object in a table. It only shows 4 elements from the object in the table. I’ve got them stored in a BeanItemContainer for my GroupContainer class.

I followed the tutorial and created a GroupForm (to match my GroupList). This is now “working” in that it follows the simple model of top-down listing of the same bean properties as the table/list.

I think that layout will not work and will prefer to build my own layout using the Form concept, but with perhaps Grid/Vertical/Horizontal layouts, etc. to put my own text fields, and the rest.

Are there examples of doing this? The tutorial forms are mostly automatic, so I’m not sure what the process is for doing this with a purely customized layout, but I still want the other aspects of the Form.

Thanks much. Vaadin is turning out to be pretty impressive from what I’ve seen so far, making it much easier than dealing with all the DTOs and RPCs and such of classical client-server.

(Sorry to have missed the obvious setLayout() method in Form. Now to give it a try…and hope the property id used by my FieldFactory will match the name given when I add components to a CustomLayout!)