final TextField tx = new TextField("Integer");
mainWin.addComponent(tx);
tx.setImmediate(true);
CompositeValidator v = new CompositeValidator();
v.addValidator(new IntegerValidator("{0} is not a number"));
v.addValidator(new Validator() {
private boolean isValid(Object value) {
try {
int i = Integer.parseInt("" + value);
if (i < 0) {
return false;
}
return true;
} catch (NumberFormatException e) {
return false;
}
}
@Override
public void validate(Object value) throws InvalidValueException {
if (!isValid(value)) {
throw new InvalidValueException(value
+ " is not a non-negative number");
}
}
});
CompositeValidator v2 = new CompositeValidator(CombinationMode.OR, null);
v2.addValidator(v);
v2.addValidator(new Validator() {
@Override
public void validate(Object value) throws InvalidValueException {
if (!"".equals("" + value)) {
throw new InvalidValueException("Value is not empty string");