setMainWindow(mainWin);
// Create form for editing address
- Form f = new Form();
+ final Form f = new Form();
f.setItemDataSource(new BeanItem(address, new String[] { "name",
"street", "zip", "city", "state", "country" }));
f.setCaption("Office address");
}
}));
+ final AddressValidator av = new AddressValidator();
+ mainWin.addComponent(new Button("Add addressvalidator",
+ new Button.ClickListener() {
+
+ public void buttonClick(ClickEvent event) {
+ f.addValidator(av);
+ }
+ }));
+ mainWin.addComponent(new Button("Remove addressvalidator",
+ new Button.ClickListener() {
+
+ public void buttonClick(ClickEvent event) {
+ f.removeValidator(av);
+ }
+ }));
+
}
/** Address pojo. */
}
}
+ class AddressValidator implements Validator {
+
+ public boolean isValid(Object value) {
+ if (!(value instanceof Address)) {
+ return false;
+ }
+ Address a = (Address) value;
+ if (a.getCity() == null || ("" + a.getCity()).length() < 1) {
+ return false;
+ }
+ if (a.getStreet() == null || ("" + a.getStreet()).length() < 1) {
+ return false;
+ }
+ if (a.getZip() == null || ("" + a.getZip()).length() < 5) {
+ return false;
+ }
+ return true;
+ }
+
+ public void validate(Object value) throws InvalidValueException {
+ if (!isValid(value)) {
+ throw new InvalidValueException(
+ "Address should at least have street, zip and city set");
+ }
+ }
+ }
+
/** Simple state validator */
class IsValidState implements Validator {