Integer Validator

Integer validator doesn’t validate an int, but its string representation as would be the case when entered from the ui.
Hence the following scenario fails.



	private void testIntValidator(Window mainWindow) {
		TextField tf = new TextField();
		tf.setImmediate(true);
		tf.addValidator(new IntegerValidator("bug?"));
		Integer val = new Integer(5000);
		tf.setValue(val.intValue());
		if (!tf.isValid()) {
			System.out.println("You are not a int :D");
		}
		mainWindow.addComponent(tf);
	}

The validator extends AbstractStringValidator hence the problem at this method.


   public boolean isValid(Object value) {
        if (value == null) {
            return true;
        }
        if (!(value instanceof String)) {
            return false;
        }
        return isValidString((String) value);
    }

This I think is a better solution. Notice that if the value is null, validation fails.



public class IntValidator extends AbstractValidator {
	
	private static final long serialVersionUID = -9203475720179860875L;

	public IntValidator(String errorMessage) {
		super(errorMessage);
	}

	@Override
	public boolean isValid(Object value) {
		if (value == null) {
			return false;
		}
		if(value instanceof Integer){
			return true;
		}
		
		if(value instanceof String){
			String strVal = (String)value;
			try {
	            Integer.parseInt(strVal);
	            return true;
	        } catch (Exception ignoredException) {
	            return false;
	        }
			
		}
		
		return false;
	}
}

6.5.0 validates Integers

Glad to know its been fixed, I’ll update at once.