ignore

Validation Annotations

Simple Type Validators

The following validators are simply present to perform type checking.

@ValidateBoolean
@ValidateByte
@ValidateShort
@ValidateInteger
@ValidateLong
@ValidateFloat
@ValidateDouble

In each case, the validator's annotation has just two attributes:

Note that these validators do not do any type converting themselves. The converting will be done by a converter that comes implicitly with the data binder in the form property's getter method, or explicitly defined using a converter annotation in the form property's getter method. The default converter is SafeBeanUtilsConverter, which is capable of performing any of the necessary conversions. In other words, the presence of a @BindSimple or @BindSelect annotation in the form property's getter method is sufficient for allowing the necessary conversion to take place.

An example for an Integer field is shown below:

private String days;
private HolidayBooking booking;

@BindSimple(expression = "booking.days")
public String getDays()
{
	return days;
}

@ValidateRequired(order = 3, key = "holidaybookingform.days.null")
@ValidateInteger(key = "holidaybookingform.days.number")
public void setDays(String days)
{
	this.days = days;
}

@DateValidator

DateValidator is almost identical to the validators defined below, except for one difference. The default converter SafeBeanUtilsConverter cannot be used for the necessary date conversion. Here, the bind annotation's converter class must either be explicitly specified, or a converter annotation must be used.

An example of the use of an explicit converter class name is shown below:

private String startDate;

private HolidayBooking booking;

@BindSimple(expression = "booking.startDate", converter = SQLDateConverter.class)
public String getStartDate()
{
	return startDate;
}

@ValidateRequired(order = 2, key = "holidaybookingform.start.date.null")
@ValidateDate(key = "holidaybookingform.start.date.format")
public void setStartDate(String startDate)
{
	this.startDate = startDate;
}

To use the @ConvertDate annotation, getStartDate() can be changed as follows:

@BindSimple(expression = "booking.startDate")
@ConvertDate(pattern = "yyyy-MM-dd")
public String getStartDate()
{
	return startDate;
}

@ValidateRequired

This validator works on the raw or unconverted value of an typed form property. It will fail if the supplied object is null. If the supplied object is a String, it will also fail if the inputted String is empty.

@ValidateBlankOrNull

This validator is performed on the raw or unconverted value of a String form property, which will fail of the inputted value is null or empty. The message parameters available are the same as for the simple type validators.

@ValidateMaxLength

This validator is performed on the raw or unconverted value of a String form property, which will fail if the length of the String exceeds the number specified using the annotation's maxLength attribute. The value for maxLength can also be used as a parameter for the validation failure message.

@ValidateIntegerRange

This validator is performed on a converted Integer, and returns false if the inputted value is outside the range specified using the annotation's min and max attributes. The values for min and max can also be used as parameters for the validation failure message.

@ValidateLongRange

This validator is performed on a converted Long, and returns false if the inputted value is outside the range specified using the annotation's min and max attributes. The values for min and max can also be used as parameters for the validation failure message.

SourceForge.net logo java.net Member logo Copyright © 2005-2007 Realsolve Solutions