This article discusses two facets of the Struts-Validation framework. The first one deals with complex rules, while the second concerns information for creating custom rules that could be hooked up with the Validation framework.
Validators: Concluding Remarks - Define the validation logic (Page 3 of 4 )
Second, you must define the validation logic. The validation is contained within a method. This method is static. It can be give any name but the parameters passed must be the references of the following:
Object:
The form bean is passed an object. The form bean contains the value of the field to be validated.
ValidatorAction:
It contains the information to dynamically instantiate and run the validation method. It represents the rule. It also represents the XML validator element that will be entered in validator-rules.xml in the third step.
Field:
It contains the list of rules, the associated form property, message arguments, messages, variables used in performing validations and generated error messages. In short, it represents the field element that is being used in validation.
ActionErrors:
When the validation fails, ActionError will have to be added to the ActionErrors.
Apart from these, the fifth parameter is HttpServletRequest. Now let's see how to put the validation logic into code.
public class PhoneRules { public static boolean validatePhone( Object bean, ValidatorAction va, Field field, ActionErrors errors, HttpServletRequest request) { : : } }
Next, get the value entered in the Phone No field using the getValueAsString() method of BeanUtils class. It takes form bean and field name as arguments. The field name could be obtained from the Field parameter.
public class PhoneRules { public static boolean validatePhone( Object bean, ValidatorAction va, Field field, ActionErrors errors, HttpServletRequest request) { //get the value entered in the field String phone = ValidatorUtil. getValueAsString(bean, field.getProperty()); : :
} }
Now the actual logic comes into the picture.
public class PhoneRules { public static boolean validatePhone( Object bean, ValidatorAction va, Field field, ActionErrors errors, HttpServletRequest request) { //get the value entered in the field String phone = ValidatorUtil. getValueAsString(bean, field.getProperty());
//Check to see if the value is a valid phone char [] chars = phone.toCharArray(); int numberCount=0;
for (int index=0; index < chars.length; index++) { char c = chars[index]; if (Character.isDigit(c)) { numberCount++; } else if (Character.isWhitespace(c)) { //White space okay } else if (c==’+’||c == '(' || c ==')' || c =='-') { if(c==’+’&&numberCount=0) ;//do nothing else return false; // ()- okay too } else { return false; } } } }
The phone number can be of the form +91 (0755) 23401551 or +91 0755-23401551 (according to the Indian telephone number pattern). It also counts the number of digits. The maximum allowed number of digits is 14. If the number of digits is more than that, the validation fails. To generate an error when the validation fails, the error has to be added to the action errors. It can be accomplished like this:
public class PhoneRules { public static boolean validatePhone( Object bean, ValidatorAction va, Field field, ActionErrors errors, HttpServletRequest request) { //get the value entered in the field String phone = ValidatorUtil. getValueAsString(bean, field.getProperty());
//Check to see if the value is a valid phone char [] chars = phone.toCharArray(); int numberCount=0;
for (int index=0; index < chars.length; index++) { char c = chars[index]; if (Character.isDigit(c)) { numberCount++; } else if (Character.isWhitespace(c)) { //White space okay } else if (c==’+’||c == '(' || c ==')' || c =='-') { // ()- okay too } else { return false; } } //If not 14 digits then not a valid phone if (numberCount != 14) {
errors.add(field.getKey(), Resources.getActionError(request, va, field);