DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Struts Custom Validation Example

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 15, 12 · · Tutorial
Like (1)
Save
Tweet
47.58K Views

Join the DZone community and get the full member experience.

Join For Free

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

Error message mobile Download career Data (computing) IT Framework Form (document)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Java Hashtable, HashMap, ConcurrentHashMap: Performance Impact
  • DZone's Article Submission Guidelines
  • OPC-UA, MQTT, and Apache Kafka: The Trinity of Data Streaming in IoT
  • Evolving Domain-Specific Languages

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo