Struts 1 > Struts Custom Validation Example

Struts Custom Validation Example

In this example we will see how to do custom validation in struts. To perform custom validation we extend the UserForm from org.apache.struts.validator.ValidatorForm. The UserForm contains two fields one for the phone number and the other for the mobile number. Lets take the senario where either phone number or mobile number is mandatory. If the validation takes into account only one field at a time then Struts Validation Framework does an excellent job. What if we need to consider more than one form field to validate data. In this case we go for custom validation.

The UserForm class contains the following code.

01.public class UserForm extends org.apache.struts.validator.ValidatorForm {
03.    private String phoneNumber;
04.    private String mobileNumber;
06.    public String getPhoneNumber() {
07.        return phoneNumber;
08.    }
10.    public void setPhoneNumber(String phoneNumber) {
11.        this.phoneNumber = phoneNumber;
12.    }
14.    public String getMobileNumber() {
15.        return mobileNumber;
16.    }
18.    public void setMobileNumber(String mobileNumber) {
19.        this.mobileNumber = mobileNumber;
20.    }
22.    public UserForm() {
23.        super();
24.    }
26.    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
27.        ActionErrors errors = super.validate(mapping, request);
28.        if ((getPhoneNumber() == null || getPhoneNumber().length() < 1) &&
29.                (getMobileNumber() == null || getMobileNumber().length() < 1)) {
30.            errors.add("phoneNumber", new ActionMessage("error.phoneNumber.required"));
31.        }
32.        return errors;
33.    }

We will override the validate method in the UserForm to perform custom validation. First we call the super class validate method inorder to save any errors returned by the ValidatorForm. Then we check whether the mobile number or phone number is present. If both are not present then we create a new ActionError and add the errors to it. After performing all the validations we will save the errors if any and return the ActionError object. If any errors are present the user will be forwarded to the input page ( in our case it's the user.jsp page).

The following message should be configured in the file. If either phone number or mobile number is not entered by the user then the following error message will be displayed.

1.error.phoneNumber.required = Either Phone number of Mobile number is required.

On runing this sample custom validation example the following page is displayed. The user needs to enter either phone number or mobile number to enter successfully.

When the user clicks the submit button without entering both phone number and mobile number then the following error message is displayed.

You can download the source code of the custom validation example by clicking on the Download link below.

Source :Download
Source + Lib :Download
Contact Us  |  Copyright 2012