Validator package

Hi everybody,

I finally got around to adding a few validators I had laying around to incubator. Just some basic stuff like integer, double, regexp and email validators. The project can be found at incubator/Validators. In case you want to use it in a project there is a jar available at
http://dev.itmill.com/browser/incubator/Validators/validators-20080211.jar
.

Use like:


TextField tf = new TextField("A textfield");
tf.addValidator(new EmailValidator("{0} is not a valid email address"));

{0} is replaced by the validated value in the error message.

Feel free to contribute more validators to the project!

When can we expect these in the official release package?

Don’t hold your breath, they won’t make it into 5.3.0. But I think we can squeeze them into the following release.

On a less-serious-and-pretty-nitpicky note, the regexp used to validate emails isn’t 100%
RFC-compliant
. A more compliant regexp would be found at e.g.
http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html
. Yeah, regexps aren’t suited for all things parsing…

On a more serious note, however: that regexp should, indeed, validate all email addresses in practical current use.

Haha, that’s insane :shock:

// Jonas

Great, these are probably the most often needed validators.

I’d like to see client-side validators as well, so that the user would get really-immediate feedback, that is, the user wouldn’t even be allowed to input invalid format. Think of “Pattern Fields”, for example.

It’s of course necessary to have server-side validation as the primary validation.

It’s possible to extend client-side components to add validation, but it would be more convenient if there was a validator interface in the client-side as well. Or a more generic decorator interface.

Or just use Hibernate Validator and use annotations on your pojo classes. There is for example Length, Pattern, Digits, Future and Email annotations.


package test;

import org.hibernate.validator.ClassValidator;
import org.hibernate.validator.InvalidValue;

import com.itmill.toolkit.data.Validator;
import com.itmill.toolkit.data.util.MethodProperty;

public class AnnotationValidator implements Validator {

	private Class clazz;
	private String propertyName;
	private ClassValidator validator;

	public AnnotationValidator(Class clazz, String propertyName) {
		this.clazz = clazz;
		this.propertyName = propertyName;
		this.validator = new ClassValidator(clazz);
	}

	public boolean isValid(Object value) {
		try {
			validateValue(value);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	public void validate(Object value) throws InvalidValueException {
		validateValue(value);
	}

	private void validateValue(Object value) {
		try {
			Object bean = clazz.newInstance();
			MethodProperty m = new MethodProperty(bean, propertyName);
			m.setValue(value);
			InvalidValue[] values = validator.getInvalidValues(bean, propertyName);
			if (values != null && values.length > 1) {
				InvalidValueException[] valueExceptions = new InvalidValueException[values.length]
;
				for(int i = 0; i < values.length; i++) {
					valueExceptions[i]
 = new InvalidValueException(values[i]
.getMessage());
				}
				throw new InvalidValueException("Annotation validator exception", valueExceptions);
			} else if(values != null && values.length == 1) {
				throw new InvalidValueException(values[0]
.getMessage());
			}
		} catch(InvalidValueException e) {
                       throw e;
              } catch(Exception e) {
			throw new InvalidValueException(e.getMessage());
		}
	}
}