Meyyappan has posted 101 posts at DZone. View Full User Profile

Struts Custom Validation Example

06.15.2012
| 26675 views |
  • submit to reddit

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.

public class UserForm extends org.apache.struts.validator.ValidatorForm {

    private String phoneNumber;
    private String mobileNumber;

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getMobileNumber() {
        return mobileNumber;
    }

    public void setMobileNumber(String mobileNumber) {
        this.mobileNumber = mobileNumber;
    }

    public UserForm() {
        super();
    }

    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = super.validate(mapping, request);
        if ((getPhoneNumber() == null || getPhoneNumber().length() < 1) &&
                (getMobileNumber() == null || getMobileNumber().length() < 1)) {
            errors.add("phoneNumber", new ActionMessage("error.phoneNumber.required"));
        }
        return errors;
    }
}

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 ApplicationResource.properties file. If either phone number or mobile number is not entered by the user then the following error message will be displayed.

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

Published at DZone with permission of its author, Meyyappan Muthuraman.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: