From 68daa4fa00300af31778e079b79ea825c12ac3be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Mon, 26 Sep 2011 15:18:31 +0200 Subject: vaadin-app: implements Validator --- .../src/ch/asynk/helloworld/HelloWorldApp.java | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/vaadin-app/src/ch/asynk/helloworld/HelloWorldApp.java b/vaadin-app/src/ch/asynk/helloworld/HelloWorldApp.java index badf223..ec7ac94 100644 --- a/vaadin-app/src/ch/asynk/helloworld/HelloWorldApp.java +++ b/vaadin-app/src/ch/asynk/helloworld/HelloWorldApp.java @@ -14,6 +14,7 @@ import com.vaadin.terminal.gwt.server.ChangeVariablesErrorEvent; import com.vaadin.data.Property; import com.vaadin.data.Property.ValueChangeEvent; import com.vaadin.data.util.ObjectProperty; +import com.vaadin.data.Validator; import com.vaadin.data.validator.DoubleValidator; import com.vaadin.ui.FormLayout; import com.vaadin.ui.VerticalLayout; @@ -69,6 +70,29 @@ class DataMappingLayout extends FormLayout { } }); // + // final Validator intValidator = new Validator() { ... } + class MyIntValidator implements Validator { + public boolean isValid(Object value) { + if (value == null || !(value instanceof String)) { + return false; + } + return ((String) value).matches("[1-9][0-9]{4}"); + } + public void validate(Object value) throws InvalidValueException { + if (!isValid(value)) { + if (value != null && value.toString().startsWith("0")) { + throw new InvalidValueException("Must not start with a zero."); + } else { + throw new InvalidValueException("Must be a number 10000-99999."); + } + } + } + }; + final TextField tf5 = new TextField("MyIntValidator"); + tf5.addValidator(new MyIntValidator()); + tf5.setImmediate(true); + addComponent(tf5); + // class IntegerObjectProperty implements Property { Integer data = 0; boolean readOnly = false; -- cgit v1.1-2-g2b99