PDA

View Full Version : MultiField and FormBinding / Form Validation



mtarantini
30 Dec 2009, 8:03 AM
Hi, I have a problem with the Multifield with FormBinding or FormValidation Validation.

I have a FormPanel with 3 Fields : name, description and a Multifield which contains another name & description field.

The model I try to bind is simple as well (see a the end of the Test Class), and I try to bind a property (description) from a ModelB contained in ModelA.

1) The problem is the binding works for the 2 first fields, but not on the MultiField.

2) When the fields from multifield are empty, and you click on the button, only the first field is valid, not both, is that a normal behavior ?

I guess i'm doing something wrong, but what?

Thanks again.



public class Test implements EntryPoint
{

/**
* This is the entry point method.
*/
public void onModuleLoad()
{
final FormPanel fp = new FormPanel();
fp.setSize(640, 200);

final TextField<String> nameField = new TextField<String>();
nameField.setName(ModelA.NAME);
nameField.setFieldLabel("Name");
nameField.setAllowBlank(false);

final TextField<String> descriptionField = new TextField<String>();
descriptionField.setName(ModelA.MODELB + "." + ModelB.DESCRIPTION);
descriptionField.setFieldLabel("Description");
descriptionField.setAllowBlank(false);

final TextField<String> nameFieldForMultiField = new TextField<String>();
nameFieldForMultiField.setName(ModelA.NAME);
nameFieldForMultiField.setAllowBlank(false);

final TextField<String> descriptionFieldForMultiField = new TextField<String>();
descriptionFieldForMultiField.setName(ModelA.MODELB + "." + ModelB.DESCRIPTION);
descriptionFieldForMultiField.setAllowBlank(false);

MultiField<String> multiField = new MultiField<String>("MultiField", nameFieldForMultiField, descriptionFieldForMultiField);
multiField.setResizeFields(true);
multiField.setSpacing(10);

fp.add(nameField, new FormData("80%"));
fp.add(descriptionField, new FormData("80%"));
fp.add(multiField, new FormData("80%"));

Button validForm = new Button("Valid Form");
validForm.addSelectionListener(new SelectionListener<ButtonEvent>()
{

@Override
public void componentSelected(ButtonEvent ce)
{
fp.isValid();
}
});

fp.addButton(validForm);

final FormBinding binding = new FormBinding(fp, true);
ModelA modelA = new ModelA("Model A", new ModelB("Description B"));
binding.bind(modelA);

RootPanel.get().add(fp);

}

private class ModelA extends BaseModel
{

/** TODO Provide comment for serialVersionUID field */
private static final long serialVersionUID = 1L;
public static final String NAME = "name";
public static final String MODELB = "modelB";

public ModelA()
{
}

public ModelA(String name, ModelB description)
{
this();
set(NAME, name);
set(MODELB, description);
}
}

private class ModelB extends BaseModel
{

/** TODO Provide comment for serialVersionUID field */
private static final long serialVersionUID = 1L;
public static final String DESCRIPTION = "desc";

public ModelB()
{
}

public ModelB(String description)
{
this();
set(DESCRIPTION, description);
}
}
}

sven
30 Dec 2009, 8:08 AM
You cannot bind a multifield as you bind it. I will move this to the help forum.

The multifield needs the name, not the two fields that are part of the multifield. You will also need to write some custom multifield binder.

You can also add the two fields that are part of the multifield dynamically to the binding.

mtarantini
30 Dec 2009, 8:12 AM
Okay thx,

But is it something that you can implement ?
Or do I have to listen to the Bind Event and bind my fields manually ?

sven
30 Dec 2009, 8:13 AM
You can also add the two fields that are part of the multifield dynamically to the binding.

That is the easiest what you can do. Simple create a fieldbinding for both fields of the multifield and add them to the binding.


What you are looking for cannot be implemented because it is too custom to work for everyone.

mtarantini
30 Dec 2009, 8:20 AM
Thx for you're help, it's working like a charm.

I add those line :



...

FieldBinding fbForName = new FieldBinding(nameFieldForMultiField, ModelA.NAME);
FieldBinding fbForDescription = new FieldBinding(descriptionFieldForMultiField,ModelA.MODELB + "." + ModelB.DESCRIPTION);

...

binding.addFieldBinding(fbForName);
binding.addFieldBinding(fbForDescription);



And it's working perfectly.

And what about the Form isValid method ? Do I have to write a custom Validator as well ?
Because it's seems to works, but only the first field of the multifield turns red if it's empty..

sven
30 Dec 2009, 8:24 AM
You will need to write some validation service for the multifield. Multifield is a "baseclass". you hsould never use it standalnoe without adding the functionality you need.

mtarantini
30 Dec 2009, 8:25 AM
Okay, understood.

Thx for the explanations!