Spring Validation
Spring allows Valdiation to be defined to setter properties of a domain object:
eg,
/**
* @spring.validator type="required"
* @spring.validator type="email"
*/
public void setEmail(String email) {
this.email = email;
}
As can be seen this are annotations that are picked up by XDoclet. See
http://xdoclet.sourceforge.net/xdoclet/tags/spring-tags.html"> XDoclet Tags for Spring.
Spring validation uses the Jakarta Commons Validation. Above email is defined to do some basic processing. This is generally setup in a validator.xml. Example for email is:
<validator name="email"
classname="org.springmodules.commons.validator.FieldChecks"
method="validateEmail"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.springframework.validation.Errors" depends=""
msg="errors.email"> <javascript><![CDATA[
function validateEmail(form) {
var bValid = true;
var focusField = null;
var i = 0;
var fields = new Array();
oEmail = new email();
for (x in oEmail) {
if ((form[oEmail[x][0]].type == 'text' ||
form[oEmail[x][0]].type == 'textarea') &&
(form[oEmail[x][0]].value.length > 0)) {
if (!checkEmail(form[oEmail[x][0]].value)) {
if (i == 0) {
focusField = form[oEmail[x][0]];
}
fields[i++] = oEmail[x][1];
bValid = false;
}
}
}
if (fields.length > 0) {
focusField.focus();
alert(fields.join('n'));
}
return bValid;
} /**
* Reference: Sandeep V. Tamhankar (stamhankar@hotmail.com),
*
http://javascript.internet.com
*/
function checkEmail(emailStr) {
if (emailStr.length == 0) {
return true;
}
var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)<>@,;:\\"\\.\\[\\]";
var validChars="[^\\s" + specialChars + "]";
var quotedUser="("[^"]*")";
var ipDomainPat=/^(d{1,3})[.](d{1,3})[.](d{1,3})[.](d{1,3})$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom + ")*$");
var matchArray=emailStr.match(emailPat);
if (matchArray == null) {
return false;
}
var user=matchArray[1];
var domain=matchArray[2];
if (user.match(userPat) == null) {
return false;
}
var IPArray = domain.match(ipDomainPat);
if (IPArray != null) {
for (var i = 1; i <= 4; i++) {
if (IPArray[i] > 255) {
return false;
}
}
return true;
}
var domainArray=domain.match(domainPat);
if (domainArray == null) {
return false;
}
var atomPat=new RegExp(atom,"g");
var domArr=domain.match(atomPat);
var len=domArr.length;
if ((domArr[domArr.length-1].length < 2) ||
(domArr[domArr.length-1].length > 3)) {
return false;
}
if (len < 2) {
return false;
}
return true;
}]]>
</javascript> </validator>When a validator is wired up, say for a web form, spring validation allows validating first at the client side using javascript (as seen above), and then again at the server side.
Masks
You can also setup masks on fields:
/**
* @spring.validator type="mask" msgkey="errors.phone"
* @spring.validator-var name="mask" value="${phone}"
*/
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
Limitations
Spring doesn't handle masks on non Strings. So either define a property validator as in email, or instroduce another property on the domain object to apply the mask on, then in the setter of this call the proper property.
eg.
BigDecimal costPrice --> mask of currency doesn't work since it is a non String.
so use: String cost --> set a mask of currency on this, then in the setter method:
costPrice = new BigDecimal(cost);
javascript validation is performed, and also on server, since costPrice = new BigDecimal(cost) will fail if it can't be converted as well.